在另一个管道的参数中添加管道阶段 - MongoDB

Add pipeline stage in the arguments of another pipeline - MongoDB

是否可以将管道添加到 mongodb 中另一个管道的参数?

我得到的当前结果示例: MongoPlayground

是否可以在将 emp table 作为参数添加到查找管道之前仅将其投影到名称和电子邮件字段? 我想要的结果:

[
  {
    "_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"
        }
    ]
}