我需要在我的 C# 代码中在 MongoDB 中运行以下查询。我使用 MongoDB.Driver 2.7.2 和 MongoDB 4.0。
我想使用 $lookup 而不是 $project / $unwind 以防止命名集合的每个可能字段。
db.getCollection('Collection1').aggregate([
{ "$match" : { "Id" : 1 } },
{ "$lookup": {
"from": "Collection2",
"let": { collection1Id : "$Id" },
"pipeline": [
{ $match:
{ $expr:
{ $and:
[
{ $eq : [ "$Collection1Id", "$$collection2Id" ] },
{ $in : [ "$_id" , [1, 2, 3]] }
]
}
}
}
],
"as": "item"
}
}
])
Run Code Online (Sandbox Code Playgroud)
我期望在多个连接条件下 Collection1 与 Collection2 连接的输出。