Mongodb聚合 - 对分组的addToSet字段求和

时间:2013-11-13 15:52:30

标签: mongodb aggregation-framework

我有以下查询:

db.listener.aggregate(
[
    { "$match" : { "location.countryName" : "Italy" } },
    { "$project" : { "location" : "$location"} },
    { "$group" : { "_id" : { "country": "$location.countryName", "city": "$location.cityName" }, "count" : { "$sum" : 1 }, "co-ords" : { "$addToSet" : { "lat" : "$location.latitude", "long" : "$location.longitude" } } } },
    { "$group" : { "_id" : "$_id.country", "cities" : { "$push" : { "city" : "$_id.city", "count" : "$count", "co-ords" : "$co-ords" } } } },
    { "$sort" : { "_id" : 1 } },
]

给出以下结果(截断):

{
"result" : [
    {
        "_id" : "Italy",
        "cities" : [
            {
                "city" : "Seriate",
                "count" : 1,
                "co-ords" : [
                    {
                        "lat" : "45.6833",
                        "long" : "9.7167"
                    }
                ]
            },
            {
                "city" : "Milan",
                "count" : 3,
                "co-ords" : [
                    {
                        "lat" : "45.4612",
                        "long" : "9.1878"
                    },
                    {
                        "lat" : "45.4667",
                        "long" : "9.2"
                    }
                ]
            },

正如您在米兰市的示例中所看到的,城市总数是3,但经度/纬度数是2。这意味着其中一个更精确的位置有两个实例,另一个有一个实例。 我现在需要修改我的查询以反映每纬度/经度的实例数以及总计数。 它可能看起来像这样:

{
                "city" : "Milan",
                "count" : 3,
                "co-ords" : [
                    {
                        "lat" : "45.4612",
                        "long" : "9.1878",
                        "total" : 2
                    },
                    {
                        "lat" : "45.4667",
                        "long" : "9.2",
                        "total" : 1
                    }
                ]
            },

我已经尝试了一些方法来实现这一点,但它永远不会出现,因为我或者Mongo会抛出错误。 有人知道最好的方法吗?

非常感谢,

尼克。

1 个答案:

答案 0 :(得分:5)

db.listener.aggregate(
[
    { "$match" : { "location.countryName" : "Italy" } },
    { "$group" : { "_id" : { "country": "$location.countryName", 
                             "city": "$location.cityName", 
                             "coords": { "lat" : "$location.latitude", "long" : "$location.longitude" } 
                   }, 
                   "count" : { "$sum" : 1 } 
                 }
    },
    { "$group" : { "_id" : { "country": "$_id.country", "city": "$_id.city" }, 
                   "coords": { "$addToSet" : { "long" : "$_id.coords.long", 
                                               "lat" : "$_id.coords.lat",
                                               "total" : "$count" 
                                             }
                             },  
                             "count" : { "$sum" : "$count" } 
                 }
    },
    { "$group" : { "_id" : "$_id.country", 
                   "cities" : { "$push" : { "city" : "$_id.city", 
                                "count" : "$count", 
                                "coords" : "$coords" } } } },
    { "$sort" : { "_id" : 1 } },
]);

您的数据输出示例:

{   "_id" : "Italy",
    "cities" : [
        {
            "city" : "Seriate",
            "count" : 1,
            "coords" : [
                {
                    "long" : "9.7167",
                    "lat" : "45.6833",
                    "total" : 1
                }
            ]
        },
        {
            "city" : "Milan",
            "count" : 3,
            "coords" : [
                {
                    "long" : "9.1878",
                    "lat" : "45.4612",
                    "total" : 1
                },
                {
                    "long" : "9.2",
                    "lat" : "45.4667",
                    "total" : 2
                }
            ]
        }
    ]
}