具有组合结果的 Mongodb 查找元素数组 [英] Mongodb lookup array of elements with combined result

查看:26
本文介绍了具有组合结果的 Mongodb 查找元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的两个文件

订单文件:

{
   "_id":"02a33b9a-284c-4869-885e-d46981fdd679",
   "context":{
      "products":[
         {
         "id": "e68fc86a-b4ad-4588-b182-ae9ee3db25e4",
         "version": "2020-03-14T13:18:41.296+00:00"
         }
      ],
   },
}

产品文档:

{
   "_id":"e68fc86a-b4ad-4588-b182-ae9ee3db25e4",
   "context":{
      "name": "My Product",
      "image": "someimage"
   },
}

所以我试图在订单文档中查找产品,但结果应包含组合字段,如下所示:

So I'm trying to do a lookup for a products in order document, but the result should contain combined fields, like so:

"products":[
             {
             "_id": "e68fc86a-b4ad-4588-b182-ae9ee3db25e4",
             "version": "2020-03-14T13:18:41.296+00:00",
             "name": "My Product",
             "image": "someimage"
             }
          ],

不确定如何执行此操作,我应该在查找之外还是在内部执行此操作?这是我的聚合

Not sure how to do this, should I do it outside of the lookup, or inside? This is my aggregation

Orders.aggregate([
{
   "$lookup":{
      "from":"products",
      "let":{
         "products":"$context.products"
      },
      "pipeline":[
         {
            "$match":{
               "$expr":{
                  "$in":[
                     "$_id",
                     "$$products.id"
                  ]
               }
            }
         },
         {
            "$project":{
               "_id":0,
               "id":1,
               "name":"$context.name"
            }
         }
      ],
      "as":"mergedProducts"
   }
},
{
   "$project":{
      "context":"$context",
      "mergedProducts":"$mergedProducts"
   }
},
]);

推荐答案

您需要通过运行 $map$arrayElemAt 从两个数组中获取一对,然后应用 $mergeObjects 结果得到一个对象:

You need to run that mapping outside of $lookup by running $map along with $arrayElemAt to get single pair from both arrays and then apply $mergeObjects to get one object as a result:

db.Order.aggregate([
    {
        $lookup: {
            from: "products",
            localField: "context.products.id",
            foreignField: "_id",
            as: "productDetails"
        }
    },
    {
        $addFields: {
            productDetails: {
                $map: {
                    input: "$productDetails",
                    in: {
                        _id: "$$this._id",
                        name: "$$this.context.name"
                    }
                }
            }
        }
    },
    {
        $project: {
            _id: 1,
            "context.products": {
                $map: {
                    input: "$context.products",
                    as: "prod",
                    in: {
                        $mergeObjects: [
                            "$$prod",
                            { $arrayElemAt: [ { $filter: { input: "$productDetails", cond: { $eq: [ "$$this._id", "$$prod.id" ] } } }, 0 ] }
                        ]
                    }
                }
            }
        }
    }
])

蒙戈游乐场

最后一步的目标是取两个数组:productsproductDetails($lookup 的输出)并找到匹配项它们之间.我们知道总是有一个匹配项,所以我们只能得到一项 $arrayElemAt 0.作为 $map 的输出,将有一个包含合并"文档的数组.

The goals of the last step is to take take two arrays: products and productDetails (the output of $lookup) and find matches between them. We know there's always one match so we can get only one item $arrayElemAt 0. As an output of $map there will be single array containing "merged" documents.

这篇关于具有组合结果的 Mongodb 查找元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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