如何在feathers js / mongoose中找到匹配的子子文档并选择一些子文档属性?

时间:2018-07-24 19:24:57

标签: node.js mongodb mongoose feathersjs

我正在使用feathersjs和feathers-mongoose,并尝试在给定日期内查找子文档。如何使用feathersjs查询或猫鼬查询找到这些。

用户架构如下:

const users = new mongooseClient.Schema({
        ... ,
        ... ,
        records: [
            {
                plans: [
                    { ... }
                ],
                subtotal: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                discount: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                total: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                paid: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                dues: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                visits: [
                    {
                        date: {type: Date, default: new Date()},
                        description: {type: String, default: ''},
                        payment: {type: Number, min: [0, 'must be a positive amount'], default: 0.0},
                        comment: {type: String, default: ''},
                    },
                ]
            }
        ]
    }

我在数据库中有以下条目:

[
        {
            "_id": "5aeb7ff33729a513c45f0486",
            ... ,
            "records": [
                {
                    "subtotal": 1800,
                    "discount": 0,
                    "total": 1800,
                    "paid": 1800,
                    "dues": 0,
                    "_id": "5b11c14b52965c1d1cf71ecb",
                    "plans": [],
                    "visits": [
                        {
                            "date": "2018-07-16T18:00:00.000Z",
                            "description": "",
                            "payment": 1800,
                            "comment": "",
                            "_id": "5b4e0d737225ad070488ed6e"
                        }
                    ]
                },
                {
                    "subtotal": 300,
                    "discount": 0,
                    "total": 300,
                    "paid": 300,
                    "dues": 0,
                    "_id": "5b4e0d3f7225ad070488ed6a",
                    "plans": [],
                    "visits": [
                        {
                            "date": "2018-07-17T15:37:35.625Z",
                            "description": "",
                            "payment": 300,
                            "comment": "",
                            "_id": "5b4e0d3f7225ad070488ed6d"
                        }
                    ]
                }
            ]
        },
        {
            "_id": "5b54e7470d707a247cee0421",
            "records": [
                {
                    "subtotal": 300,
                    "discount": 0,
                    "total": 300,
                    "paid": 300,
                    "dues": 0,
                    "_id": "5b54e77d0d707a247cee0422",
                    "plans": [],
                    "visits": [
                        {
                            "date": "2018-07-22T20:22:21.830Z",
                            "description": "",
                            "payment": 300,
                            "comment": "",
                            "_id": "5b54e77d0d707a247cee0425"
                        }
                    ]
                }
            ]
        }
    ]

我只希望获得那些访问次数大于2018-07-17T15:37:35

的访问

我使用了羽毛查找方法,如果其中任何一个匹配,则返回所有visits子文档。 feathersjs定制服务实现如下:

async find(params) {
        return this.service.find({
            query: {
                'records.visits.date': {
                    $gte: '2018-07-17T15:37:35'
                },
                $select: ['records._id', 'records.visits']
            }
        });

    }

我使用了猫鼬查找方法。它仅返回匹配的vistis,但返回records的其他属性。猫鼬查找实现如下:

async find(params) {
        return Promise.resolve(this.model.find({
                "records.visits.date": {
                    $gte: '2018-07-17T15:37:35'
                }
            }, {
                'records.visits.$': 1
            }).select('records.visits'))
    }

我如何使用feathersjs或猫鼬查找方法获得以下预期结果?预先感谢。

[
        {
            "_id": "5aeb7ff33729a513c45f0486",
            "records": [
                {
                    "_id": "5b4e0d3f7225ad070488ed6a",
                    "visits": [
                        {
                            "date": "2018-07-17T15:37:35.625Z",
                            "description": "",
                            "payment": 300,
                            "comment": "",
                            "_id": "5b4e0d3f7225ad070488ed6d"
                        }
                    ]
                }
            ]
        },
        {
            "_id": "5b54e7470d707a247cee0421",
            "records": [
                {
                    "_id": "5b54e77d0d707a247cee0422",
                    "visits": [
                        {
                            "date": "2018-07-22T20:22:21.830Z",
                            "description": "",
                            "payment": 300,
                            "comment": "",
                            "_id": "5b54e77d0d707a247cee0425"
                        }
                    ]
                }
            ]
        }
    ]

0 个答案:

没有答案