5

I am new to Mongo! Please help me how to do left join in Mongo

Sql Statement :

Select * from TableA left Join TableB 
on (TableA.col1 = TableB.col1 AND TableB.col2 = "ABC")

Please provide me the equivalent Mongo Query!!!

Thanks In Advance !

2

1 Answer 1

8

As of Mongo 3.2, you can do the equivalent to a left outer join with the new $lookup operator added to the aggregation pipeline: https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup

Your query would become something like this:

db.TableB.aggregate([
{
  $match:{col2:"ABC"}
},
{
   $lookup:
   {
       from: "TableA",
       localField: "col1",
       foreignField: "col1",
       as: "aliasForTable1Collection"
   }
}
])

Not the answer you're looking for? Browse other questions tagged or ask your own question.