How to do Left Join in DB (Mongo) [duplicate]

落爺英雄遲暮 提交于 2019-12-13 17:36:45

问题


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 !


回答1:


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"
   }
}
])


来源:https://stackoverflow.com/questions/46958465/how-to-do-left-join-in-db-mongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!