如何对 MongoDB 中所有文档的第三级嵌套对象数组的值求和?

How to sum values of third level nested array of objects across all documents in MongoDB?

我有一个具有以下架构的猫鼬文档:

产品

{
   "section":"",
   "category":"Food & Drink",
   "sub_category":"Main Dish",
   "product_code":"ST",
   "title":"Steak",
   "description":"Served with sauted vegetables",
   "tags":[
      
   ],
   "warranty":"None",
   "product_variants":[
      {
         "variant_code":"ST1",
         "variant_title":"Rib Eye",
         "images":[
            
         ],
         "status":"Active",
         "variant_details":[
            {
               "size":"6oz",
               "local_price":800,
               "local_discount":"0",
               "foreign_price":0,
               "foreign_discount":"0",
               "inventory":[
                  {
                     "branch_id":{
                        
                     },
                     "quantity":94
                  }
               ]
            },
            {
               "size":"10oz",
               "local_price":1000,
               "local_discount":"0",
               "foreign_price":0,
               "foreign_discount":"0",
               "inventory":[
                  {
                     "branch_id":{
                        
                     },
                     "quantity":147
                  }
               ]
            },
            {
               "size":"12oz",
               "local_price":1200,
               "local_discount":"0",
               "foreign_price":0,
               "foreign_discount":"0",
               "inventory":[
                  {
                     "branch_id":{
                        
                     },
                     "quantity":199
                  }
               ]
            }
         ]
      }
   ]
}

以上文档在 product_variants 字段中只显示了一个对象,但请注意,也可以有多个对象。我需要对每个尺寸和产品变体的数量求和。

我如何使用聚合函数来做到这一点?我在节点 js 环境中使用猫鼬。

您可以使用此聚合查询:

  • $project 以仅获取 quantity 值。它生成以下输出:
"array": [
      [
        [
          94
        ],
        [
          147
        ],
        [
          199
        ]
      ]
  • 所以下一步就是用$unwind三次来平整数组。
  • $group 通过 _id 使用 $sum
yourModel.aggregate([{
  "$project": {
    "array": "$product_variants.variant_details.inventory.quantity"
  }
},
{
  "$unwind": "$array"
},
{
  "$unwind": "$array"
},
{
  "$unwind": "$array"
},
{
  "$group": {
    "_id": "$_id",
    "size": {
      "$sum": "$array"
    }
  }
}])

示例here

编辑
作为 Takis _ suggested into the comments if you want to get all values from your entire collection (not only for each document) you can $group using null as this example

查询 (它基于上一个答案中的最后一条评论,类似的查询,但将该数量乘以当地价格)

Test code here

db.collection.aggregate([
  {
    "$unwind": "$product_variants"
  },
  {
    "$unwind": "$product_variants.variant_details"
  },
  {
    "$unwind": "$product_variants.variant_details.inventory"
  },
  {
    "$set": {
      "total_local_price": {
        "$multiply": [
          "$product_variants.variant_details.inventory.quantity",
          "$product_variants.variant_details.local_price"
        ]
      }
    }
  },
  {
    $group: {
      _id: null,    // or "$_id" if you want only for 1 document
      total_qty: {
        $sum: "$total_local_price"
      }
    }
  }
])