【发布时间】:2021-07-18 01:04:32
【问题描述】:
我试图在使用聚合 $lookup 后填充第二个嵌套数组。
这是我原来的数组。
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : [
{
"_id" : ObjectId("6065de3aa95e721f587f7528")
},
]
}
{
from: "medications",
localField: "medications._id",
foreignField: "_id",
as: "medications",
}
使用聚合 $lookup 后,我得到了这个结果,但我仍然有需要填充的嵌套数组“库存”。我尝试添加另一个 $lookup 管道,但我不知道如何正确执行此操作。
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : {
"_id" : ObjectId("6065de3aa95e721f587f7528"),
"medication": "med 1"
"schedule": ["9am", "10pm"]
"inventory": "ObjectId("6076d55ab6aeb947dca85877")"
}
}
这是库存物品
{
"_id" : ObjectId("6076d55ab6aeb947dca85877"),
"item": "Inventory item 1",
"Qty": "10"
}
预期输出
{
"_id" : ObjectId("607da9c0c7cb26384c7810a6"),
"date" : ISODate("2021-04-20T00:00:00.000+0000"),
"clientID" : "601e6dc61766587af8ce76db",
"medications" : {
"_id" : ObjectId("6065de3aa95e721f587f7528"),
"medication": "med 1"
"schedule": ["9am", "10pm"]
"inventory": {
"_id" : ObjectId("6076d55ab6aeb947dca85877"),
"item": "Inventory item 1",
"Qty": "10"
}
}
}
【问题讨论】: