How to do Left Join in DB (Mongo)

17,805

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"
   }
}
])
Share:
17,805
Raghaven 3534
Author by

Raghaven 3534

I am a sportive and jolly guy :)

Updated on June 27, 2022

Comments

  • Raghaven 3534
    Raghaven 3534 almost 2 years

    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 !