猫鼬聚合与查找和对象数组

时间:2019-01-29 15:29:03

标签: node.js mongodb mongoose

我有以下收藏

 // collection: appointments
{
"_id" : ObjectId("5c50682b663e854a1c2d9401"),
"status" : "Pending",
"discount" : 0,
"removed" : false,
"services" : [
    {
        "_id" : ObjectId("5c505a29af3a655b98812ca7"),
        "service" : ObjectId("5c505a12af3a655b98812ca5"),
        "cost" : 200
    },
    {
        "_id" : ObjectId("5c50691ab9081f53287d2354"),
        "service" : ObjectId("5c5069a600ec0d7a1800aa73"),
        "cost" : 200
    }
],
"doctor" : ObjectId("5c5059b2af3a655b98812ca1"),
"patient" : ObjectId("5c5059e5af3a655b98812ca4"),
"date" : ISODate("2018-11-12T00:00:00.000+02:00"),
"clinic" : ObjectId("5c5059d8af3a655b98812ca3"),
"diagnosis" : [ ],
"rx" : [ ],
"labs" : [ ],
"scans" : [ ],
"__v" : 0
}

我正在尝试汇总该集合,但是我想填充services.service,因为它是一个对象ID

    let appointments = await Appointment.aggregate([
        { $lookup: { from: 'services',localField: 'services.service',foreignField: '_id',as: 'services' } },
        { $project: { 
            'date':                1 ,
            'status':              1 ,
            'services':            1 ,
        } },
        { $limit: Number(req.query.limit) },
        { $skip: Number(req.query.skip) }
    ]);

我会得到

 "appointments": [
    {
        "_id": "5c50682b663e854a1c2d9401",
        "status": "Pending",
        "discount": 0,
        "paidAmount": 0,
        "services": [
            {
                "_id": "5c505a12af3a655b98812ca5",
                "removed": false,
                "name": "kashf",
                "clinic": "5c5059d8af3a655b98812ca3",
                "updatedAt": "2019-01-29T13:50:10.651Z",
                "createdAt": "2019-01-29T13:50:10.651Z",
                "__v": 0
            },
            {
                "_id": "5c5069a600ec0d7a1800aa73",
                "removed": false,
                "name": "arza3",
                "clinic": "5c5059d8af3a655b98812ca3",
                "updatedAt": "2019-01-29T14:56:38.314Z",
                "createdAt": "2019-01-29T14:56:38.314Z",
                "__v": 0
            }
        ],
        "date": "2018-11-11T22:00:00.000Z"
    }
]

所以我丢失了cost属性,也失去了对象数组的id 有什么解决办法吗? 我尝试展开服务,但结果是两个约会对象的ID相同

2 个答案:

答案 0 :(得分:1)

我知道了

let appointments = await Appointment.aggregate([

        { $unwind: '$services' },
        { $lookup: { from: 'services',localField: 'services.service',foreignField: '_id',as: 'services.service' } },
        { $unwind: '$services.service' },
        {
            $group: {
                '_id':                 '$_id',
                'services':            { $push: '$services' },

            }
        },
        { $project: { 

            'services':            1 ,

        } },

    ]);

答案 1 :(得分:0)

u可以使用多个填充项

CHAR.findOneAndUpdate({id: info.id}, char, {upsert: true, new: true})
    .populate({path : 'intels', populate : {path : 'intels', populate : {path : 'from'}}})
    .populate({path : 'alts', populate : {path : 'alts', populate : {path : 'intels', populate : {path : 'intels.from'}}}})