集合A:
[{
"_id": 1,
"operation":"SEC",
"name":"x"
},{
"_id": 2,
"operation": "SEC",
"name": "y"
},
{
"_id": 3,
"operation": "SEC",
"name": "z"
}]
集合B:
[{
"user": 1,
"operation":"SEC",
"name":"x",
"date": "2022-10-25"
},{
"user": 2,
"operation":"SEC",
"name":"y",
"date": "2022-10-25"
}
]
预期输出:
[
{
"_id": 3,
"operation": "SEC",
"name": "z"
}
]
我有两个集合,我想按日期从第一个集合匹配到第二个集合,只想得到不在第二个集中的集合。
您可以使用以下聚合管道来实现您想要的输出:
[
{
"$lookup": {
"from": "collectionB",
"localField": "_id",
"foreignField": "user",
"as": "collectionB"
}
},
{
$match: {
collectionB: {
$size: 0
}
}
},
{
$project: {
collectionB: 0
}
}
]
请注意,这是一个有效的解决方案。您可能应该在开始时添加一个$match
步骤,以限制您的结果。