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
  • 2
    MongoDB tells you how to join and you can see how to chain in this duplicate post Commented Oct 26, 2017 at 16:03
  • @Raghaven was the answer helpful? Feel free to upvote&accept. Commented Mar 27, 2018 at 22:49

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"
   }
}
])
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.