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

发布于 2025-01-19 02:46:06 字数 570 浏览 1 评论 0原文

是否可以将管道添加到MongoDB中其他管道的参数中?

我得到的当前结果的一个例子: mongoplayground

是否可以将EMP表投射到emp表格并将其添加到查找管道之前,作为争论? 我想要的结果:

[
  {
    "_id": ObjectId("610bce417b0c4008346547bc"),
    "employees": [
      {
        "email": "[email protected]",
        "name": "xyz"
      }
    ],
    "name": "Chicago",
    "number": 10
  }
]

Is it possible to add a pipeline to the arguments of another pipeline in mongodb?

An example of the current result that I am getting:
MongoPlayground

Is it possible to project the emp table to only name and email field before adding it to the lookup pipeline as an argument?
The result that I want:

[
  {
    "_id": ObjectId("610bce417b0c4008346547bc"),
    "employees": [
      {
        "email": "[email protected]",
        "name": "xyz"
      }
    ],
    "name": "Chicago",
    "number": 10
  }
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2025-01-26 02:46:07

你快到了。您寻求的是 $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" : "[email protected]"
        }
    ]
}

You are almost there. What you seek is the pipeline version of $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"
   }}
]);

which will yield:

{
    "_id" : 0,
    "name" : "Chicago",
    "number" : 10,
    "employees" : [
        {
            "name" : "xyz",
            "email" : "[email protected]"
        }
    ]
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文