I need to run the following query in MongoDB in my C# code. I use MongoDB.Driver 2.7.2 and MongoDB 4.0.
I would like to use $lookup rather than $project / $unwind in order to prevent naming each one of the collection's possible fields.
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"
}
}
])
I expect the output of Collection1 joined with Collection2 under multiple join conditions.
As you didn't specify a particular class mapping and example documents, below answer will use
BsonDocument
type and example data from the manual $lookup: specify multiple join conditions with lookupPlease note that support for more expressive $lookup using PipelineDefinitionBuilder is coming for version 2.8.x. For more information see CSHARP-2013