如何在数据库中进行左连接(Mongo) [英] How to do Left Join in DB (Mongo)

查看:26
本文介绍了如何在数据库中进行左连接(Mongo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 !

解决方案

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

这篇关于如何在数据库中进行左连接(Mongo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆