收藏中的文件
{ "_id" : "01001",
"city" : "AGAWAM",
" loc" : [ -72.622739, 42.070206 ],
"pop" : 15338,
"state" : "MA" }
第一部分是找到每个州最高的城市。 zipcodes(_id's)
我有: -
db.zips.aggregate([{
$group:
{
_id: {state:"$state",city: "$city" } ,
count: {$sum:1},
population:{$sum:"$pop"}
}
},
{
$sort:{count:-1}
}
])
我得到的输出: -
{ "_id" : { "state" : "TX", "city" : "HOUSTON" }, "count" : 93, "population" : 2095918 }
{ "_id" : { "state" : "CA", "city" : "LOS ANGELES" }, "count" : 56, "population" : 2102295 }
{ "_id" : { "state" : "PA", "city" : "PHILADELPHIA" }, "count" : 48, "population" : 1610956 }
{ "_id" : { "state" : "IL", "city" : "CHICAGO" }, "count" : 47, "population" : 2452177 }
{ "_id" : { "state" : "TX", "city" : "SAN ANTONIO" }, "count" : 45, "population" : 811792 }
{ "_id" : { "state" : "TX", "city" : "DALLAS" }, "count" : 44, "population" : 940191 }
{ "_id" : { "state" : "MO", "city" : "KANSAS CITY" }, "count" : 41, "population" : 360182 }
问题是我需要找到排名最高的城市。每个州的邮政编码,因此每个州只应出现一次而不是排序列表。基本上我正在为每个州寻找最高价值的邮政编码。
有关如何实现这一目标的任何想法?
答案 0 :(得分:1)
只需在汇总管道中添加第二个$ group阶段,如下所示:
db.zips.aggregate([
{
$group:{
_id:{
state:"$state",
city:"$city"
},
count:{
$sum:1
},
population:{
$sum:"$pop"
}
}
},
{
$sort:{
count:-1
}
},
{
$group:{
_id:"$_id.state",
count:{
$first:"$count"
},
city:{
$first:"$_id.city"
},
population:{
$first:"$population"
}
}
}
])