从MongoDB中重复文档的集合中的唯一文档获取值的总和

时间:2019-11-21 07:46:45

标签: javascript node.js mongodb mongoose

我是MongoDB的新手,并且陷入以下情况。

我有一个包含重复文档的集合。 我只想获取每个文档中属性的总和,不包括重复的文档。

我的文档如下:

{"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",
"sellingId":"319723fb80b1a297cf0803abad9bc60787537f14a6a37d6e47",
"account_name":"mrfsahas1234",
"vendor_name":"testaccount2",
"action_type":"purchase",
"product_name":"Bottle",
"product_quantity":10,
"transactionId":"319723fb80b1a297cf0803abad9bc60787537f14a6a37d6e47",
"uid":"2019-11-20T17:39:17.405Z",
"createdAt":"2019-11-21T08:56:56.589+00:00",
"updatedAt":"2019-11-21T08:56:56.589+00:00","__v":0
},
{
"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",  
"sellingId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
 "account_name":"mrfsahas1234",
 "vendor_name":"testaccount2",
 "action_type":"purchase",
 "product_name":"Bottle",
 "product_quantity":50, 
 "transactionId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
 "uid":"2019-11-20T17:39:17.405Z",
},
{
"_id":"5dd629461fc50b782479ea90",
"referenceId":"5dd581f10859d2737965d23a",  
"sellingId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
 "account_name":"mrfsahas1234",
 "vendor_name":"testaccount2",
 "action_type":"purchase",
 "product_name":"Bottle",
 "product_quantity":50, 
 "transactionId":"320a9a2f814a45e01eb98344c9af708fa2864d81587e5914",
 "uid":"2019-11-20T17:39:17.405Z",
},

当前,我正在这样做:

MaterialsTrack.aggregate([
            {
                $match: {
                    $and: [
                        {product_name: product_name},
                        {account_name: account_name},
                        {action_type: 'purchase'},
                        {uid:uid}
                    ]
                }
            },
            {
                $group: {_id: "$sellingId", PurchseQuantity: {$sum: "$product_quantity"}}
            },
        ])

它返回product_quantity所有匹配文档(包括重复文档)的总和。 当前输出:

{_id: "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914", PurchseQuantity:110}

预期输出:

{_id: "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914", PurchseQuantity:60}

我只想获取唯一文档的总和。我该如何实现? 预先感谢!

2 个答案:

答案 0 :(得分:1)

如何将$ addToSet添加到聚合管道

MaterialsTrack.aggregate([
            {
                $match: {
                    $and: [
                        {product_name: product_name},
                        {account_name: account_name},
                        {action_type: 'purchase'},
                        {uid:uid}
                    ]
                }
            },
            {
                $group: {_id: "$sellingId", PurchseQuantity: {$sum: "$product_quantity"},"list" : {$addToSet : "$list"}}
            },
        ])

答案 1 :(得分:1)

您需要在$ group _id字段内求和,然后使用replaceRoot获得所需的结果。

MaterialsTrack.aggregate([
  {
    $match: {
      $and: [
        {
          product_name: "Bottle"
        },
        {
          account_name: "mrfsahas1234"
        },
        {
          action_type: "purchase"
        },
        {
          uid: "2019-11-20T17:39:17.405Z"
        }
      ]
    }
  },
  {
    $group: {
      _id: {
        sellingId: "$sellingId",
        PurchaseQuantity: {
          $sum: "$product_quantity"
        }
      }
    }
  },
  {
    $replaceRoot: {
      newRoot: {
        _id: "$_id.sellingId",
        PurchaseQuantity: "$_id.PurchaseQuantity"
      }
    }
  }
]);

样本输出:

[
  {
    "PurchaseQuantity": 50,
    "_id": "320a9a2f814a45e01eb98344c9af708fa2864d81587e5914"
  }
]

游乐场:

https://mongoplayground.net/p/MOneCRiSlO0