【问题标题】:Mongoose aggregate with lookup and object array带有查找和对象数组的 Mongoose 聚合
【发布时间】:2019-06-22 18:40:27
【问题描述】:

我的收藏如下

 // 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"
    }
]

所以我丢失了成本属性,也丢失了对象数组的 id 有什么解决方案吗? 我尝试展开服务,但它导致两个约会对象具有相同的 id

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案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 ,
    
            } },
    
        ]);
    

    【讨论】:

      【解决方案2】:

      你可以像这样使用多个填充

      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'}}}})
      

      【讨论】:

      • 是的,但是我使用聚合来查找具有特定姓名的患者,所以我需要它,比如内部连接
      • 哦,好吧,我从不使用聚合 sry =(
      猜你喜欢
      • 2020-07-16
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 2020-08-20
      • 2021-07-23
      • 2021-08-23
      • 2020-01-24
      相关资源
      最近更新 更多