MongoDB - 复合组

时间:2016-02-17 19:15:32

标签: mongodb pymongo

我有城市文件集,我的文件如下:

{
    "_id" : ObjectId("52fe1d364b5ab856eea75ebc"),
    "elevation" : 1855,
    "name" : "Kud",
    "country" : "India",
    "lon" : 75.28,
    "lat" : 33.08,
    "isPartOf" : [
        "Jammu and Kashmir",
        "Udhampur district"
    ],
    "timeZone" : [
        "Indian Standard Time"
    ],
    "population" : 1140
}

我有兴趣找到"城市集合中所有国家的平均区域城市人口"。我的聚合管道命令如下:

pipeline = [
            {"$match": {"isPartOf":{"$exists":1}, "country":{"$exists":1}}},
            {"$unwind": "$isPartOf"},
            {"$group":{"_id": {"isPartOf": "$isPartOf", "country": "$country"}, "avgRegionalPopulation": {"$avg":"$population"}}},
            {"$sort": {"avgRegionalPopulation":-1}}
            ]

但似乎我错过了一些东西。因为' avgRegionalPopulation'为立陶宛'应该是14750.784447977203。我的管道命令中缺少什么?

1 个答案:

答案 0 :(得分:0)

您错过了每个国家/地区的平均人口数。这是一个正确的聚合管道命令:

pipeline = [
            {"$match": {"isPartOf":{"$exists":1}, "country":{"$exists":1}}},
            {"$unwind": "$isPartOf"},
            {"$group" : {"_id": {"isPartOf": "$isPartOf", "country": "$country"}, "avgregion":{"$avg":"$population"}}},
            {"$group": {"_id": "$_id.country", "avgRegionalPopulation": {"$avg":"$avgregion"}}},
            {"$sort": {"country":-1}}
            ]