【发布时间】: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
【问题讨论】: