Reputation: 21
How do I perform a left outer join while accessing data from multiple collections? I want to get the details from collection even if there is no match in DB_Market. Presently if I filter further using condition, I am able to achieve inner join. However I also need left outer in addition to this.
Sample code
db.new.aggregate([
{
"$lookup": {
"from": "DB_Market",
"localField": "var1",
"foreignField": "var1",
"as": "collection2_doc"
}
},
{"$unwind": "$collection2_doc" }])
Upvotes: 0
Views: 83
Reputation: 985
I think you can perform left outer join. When you are doing $unwind
, make sure preserveNullAndEmptyArrays is true
db.new.aggregate([
{
$lookup: {
"from": "DB_Market",
"localField": "var1",
"foreignField": "var1",
"as": "collection2_doc"
}
},
{$unwind: { path: "$collection2_doc", preserveNullAndEmptyArrays: true } }
])
Upvotes: 1