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

mongodb aggregation sums of elements over all(specific) arrays in collection

也许有人可以帮我解决这个问题。

我有一个包含如下数据的集合:

{
    "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 ] }
...

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

非常感谢!

这可以使用两个连续的 $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 } ] }