【问题标题】:Mongodb lookup array of elements with combined resultMongodb查找具有组合结果的元素数组
【发布时间】:2020-06-29 14:34:24
【问题描述】:

这是我的两份文件

订单文件:

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

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

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

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

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"
   }
},
]);

【问题讨论】:

    标签: node.js database mongodb aggregation-framework mongodb-lookup


    【解决方案1】:

    您需要在 $lookup 之外运行该映射,方法是运行 $map$arrayElemAt 以从两个数组中获取一对,然后应用 $mergeObjects 以获取一个对象:

    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 ] }
                            ]
                        }
                    }
                }
            }
        }
    ])
    

    Mongo Playground

    最后一步的目标是获取两个数组:productsproductDetails$lookup 的输出)并找到它们之间的匹配项。我们知道总会有一场比赛,所以我们只能得到一件物品$arrayElemAt 0。作为$map 的输出,将有一个包含“合并”文档的数组。

    【讨论】:

    • 非常感谢您,好心的先生。
    • 不错。在最后阶段值得一些 cmets。
    • @BuzzMoschetti 谢谢,如果现在更清楚了,请查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2019-02-02
    • 2014-11-15
    相关资源
    最近更新 更多