我是Mongo的新手!请帮助我如何在Mongo中进行左加入
Sql声明:
Select * from TableA left Join TableB
on (TableA.col1 = TableB.col1 AND TableB.col2 = "ABC")
请提供相应的Mongo查询!!!
提前致谢!
答案 0 :(得分:5)
从Mongo 3.2开始,你可以使用新的$ lookup运算符添加到聚合管道,相当于左外连接: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"
}
}
])