mongodb聚合集合中所有(特定)数组的元素总和

时间:2015-01-22 11:07:22

标签: mongodb aggregation-framework

也许有些人可以帮助我。

我有一个包含这样的数据的集合:

{
    "id" : 123,
    "country" : "US",
    "timestamp" : 1417904130
}
{
    "id" : 123,
    "country" : "DE",
    "timestamp" : 1417904136
}
{
    "id" : 111,
    "country" : "US",
    "timestamp" : 1417904512
}
{
    "id" : 111,
    "country" : "US",
    "timestamp" : 1417904777
}
...

我试过了:

db.countries_usg.aggregate( [ { $match: {}}, { $group: {'_id': "$id", 'countries': {'$addToSet': '$country'} }} ], {'allowDiskUse': true})

但这只能给我:

{ "_id" : 123, "languages" : [ "US", "DE" ] }
{ "_id" : 111, "languages" : [ "US" ] }
...

我需要这个:

{ "_id" : 123, "languages" : [ "US": 1, "DE": 1 ] }
{ "_id" : 111, "languages" : [ "US": 2 ] }
...

或类似的。有人知道如何解决这个问题吗?

非常感谢!

1 个答案:

答案 0 :(得分:1)

可以使用两个连续的$ group聚合来实现:

db.countries_usg.aggregate([
        { $group: {
            '_id': {id:"$id", "country" : "$country"},
            "count" : { "$sum": 1 }
        }},
        { $group: {
            '_id': "$_id.id",
            "countries" : {"$push" : {country:"$_id.country", count : "$count"}}
        }},
    ],
    {'allowDiskUse': true}
);

结果将如下所示:

{ "_id" : 123, "countries" : [ { "country" : "DE", "count" : 1 }, { "country" : "US", "count" : 1 } ] }
{ "_id" : 111, "countries" : [ { "country" : "US", "count" : 2 } ] }