是否可以将一个管道添加到mongodb中另一个管道的参数中?
我得到的当前结果示例:MongoPlayground
在将emp表作为参数添加到查找管道之前,是否可以将其仅投影到名称和电子邮件字段?我想要的结果:
[
{
"_id": ObjectId("610bce417b0c4008346547bc"),
"employees": [
{
"email": "xyz@gmail.com",
"name": "xyz"
}
],
"name": "Chicago",
"number": 10
}
]
您就快到了。您所寻求的是$lookup
:的管道版本
db.dept.aggregate([
{$lookup: {"from": "emp",
let: { did: "$_id" },
pipeline: [
{$match: {$expr: {$eq: [ "$_id", "$$did" ]} }},
{$project: {
_id:false,
email:true,
name:true
}}
],
as: "employees"
}}
]);
将产生:
{
"_id" : 0,
"name" : "Chicago",
"number" : 10,
"employees" : [
{
"name" : "xyz",
"email" : "xyz@gmail.com"
}
]
}