4

我有以下查询:

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 会抛出一个错误。有人知道最好的方法吗?

非常感谢,

缺口。

4

1 回答 1

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
                }
            ]
        }
    ]
}
于 2013-11-13T18:01:23.963 回答