【发布时间】:2022-02-23 05:36:01
【问题描述】:
我有两个收藏
collection1 : [
{
_id:"90b992b7-b85f-4e1c-9080-fc3e2dba0db0",
pIds: [ "47c9124d-f027-4221-8d60-9f491993d923", "08c89e83-ad62-443c-a731-3c6ccd9ca3af" ]
}
]
和
collection2 : [
{
_id: "47d44016-ff84-44dc-b650-abab36d5f661",
userId: "47c9124d-f027-4221-8d60-9f491993d923",
isOnline: true
}
]
collection2 有"47c9124d-f027-4221-8d60-9f491993d923" 这个userId 的数据,但是collection2 中没有"08c89e83-ad62-443c-a731-3c6ccd9ca3af" 这个userId 的数据。
现在想要下面的输出
output : [
{
_id:"90b992b7-b85f-4e1c-9080-fc3e2dba0db0",
pIds: [
{
userId: "47c9124d-f027-4221-8d60-9f491993d923",
isOnline: true
},
{
userId: "08c89e83-ad62-443c-a731-3c6ccd9ca3af",
isOnline: false
},
]
}
]
试过
$lookup: {
from: "collection2",
let: { pIds: "$pIds" },
pipeline: [
{ $match: { $expr: { $in: ["$userId", "$$pIds"] } } },
{ $project: { _id: 0 } },
],
as: "pId",
}
任何解决方案都会有很大帮助....
提前致谢
【问题讨论】:
标签: mongodb mongodb-query nosql aggregation-framework