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

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

问题描述

我是 Mongo 的新手!请帮助我如何在 Mongo 中左加入

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

Sql 语句:

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

请提供等效的 Mongo 查询!!!

Please provide me the equivalent Mongo Query!!!

提前致谢!

推荐答案

从 Mongo 3.2 开始,您可以使用添加到聚合管道的新 $lookup 运算符执行与左外连接等效的操作:https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup

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

你的查询会变成这样:

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

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

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