根据其id聚合嵌套数组

时间:2015-12-01 11:12:00

标签: mongodb mongodb-query aggregation-framework

我有相同结构的文档,我想使用$ sum聚合成一个。这是我的文档的一个例子。

{
  "_id" : 50,
  "revenue" : 5000
  "spendings" :[
  {
     "id" : 1
     "catalogue" [
      {
         "id": 5
         "viewcount": 150
         "value": 10
      },
      {
         "id": 6
         "viewcount": 500
         "value": 20
      }
     ]
  }, 
  {
     "id": 2
     "catalogue" [
      {
         "id": 3
         viewcount: 150
         value: 10
      }
     ]
  }
}

{
  "_id" : 51,
  "revenue" : 6000
  "spendings" :[
  {
     "id" : 1
     "catalogue" [
      {
         "id": 5
         "viewcount": 100
         "value": 5
      },
      {
         "id": 6
         "viewcount": 400
         "value": 15
      }
     ]
  }, 
  {
     "id": 2
     "catalogue" [
      {
         "id": 3
         viewcount: 600
         value: 30
      }
     ]
  }
}

我想在这两个文档中创建一个文档,我使用$ sum将相应的ID加在一起。结果应该是这样的。

{
  "_id" : 52,
  "revenue" : 11000
  "spendings" :[
  {
     "id" : 1
     "catalogue" [
      {
         "id": 5
         "viewcount": 250
         "value": 15
      },
      {
         "id": 6
         "viewcount": 900
         "value": 35
      }
     ]
  }, 
  {
     "id": 2
     "catalogue" [
      {
         "id": 3
         viewcount: 750
         value: 40
      }
     ]
  }
}

但只使用一个数组。是否有可能将数组分组到另一个数组中?

到目前为止我尝试了什么:

db.conversion.aggregate([
    { "$match": {  "revenue" : { "$gte" : 5000 , "$lte" : 6000 } } },
    { "$unwind": "$spendings" },
    { "$unwind": "$spendings.catalogue" },
    {
        "$group": {
            "_id": { "spendid": "$spendings.id", "catalogueid": "$spendings.catalogue.id"},
            "rv": { "$sum": "$revenue" },
            "catvw": { "$sum": "$spendings.catalogue.viewcount" },
            "catval": { "$sum": "$spendings.catalogue.value" }
        }
    },
    {
        "$group": {
            "_id": null,
            "rev": { "$first": "$rv" },
            "spendings": { 
                "$push": {
                    "id": "$spendid",
                    "catid": "$"
                    "catview": "$catvw" ,
                    "catvalue": "$catval" 
                }
            }        
        }
    }
])

但这不起作用

1 个答案:

答案 0 :(得分:1)

我想在我的应用程序中做类似的事情,所以这是我的解决方案。我试验了一段时间,这种方法对我有用。

{ "$match": {  "revenue" : { "$gte" : 5000 , "$lte" : 6000 } } },
{ "$unwind": "$spendings" },
{
    "$group": {
        "_id": "$spendings.id", 
        "rev": { "$sum": "$revenue"},
        "spend": { "$push": "$spendings" }
    }
},
{ "$unwind": "$spend" },
{ "$unwind": "$spend.catalogue" },
{
    "$group": {
        "_id": {"catid": "$spend.catalogue.id", "spendid": "$_id" }
        "revenue": { "$first": "$rev"},
        "vcsum": {$sum: "$spend.catalogue.viewcount"},
        "valuesum": {$sum: "$spend.catalogue.value"},
    }
},
{
    "$group": {
        "_id": "$_id.spendid",
        "revenues": { "$first": "$revenue"},
        "catalogues": {
                "$push": {
                "_id": "$_id.catid",
                "viewcount": "$vcsum",
                "value": "$valuesum",
                }
            }
    }
},
{
    "$group": {
        "_id": null,
        "revenues": { "$first": "$revenues"},
        "spendings": {
                "$push": {
                    "catalogue": "$catalogues"
                }
            }
    }
}

也许这有帮助。