我有一个看起来像这样的文件:
{
"_id" : ObjectId("527a6b7c24a8874c078b9d10"),
"day" : 6,
"hour" : 15,
"hourlyLocations" : [
{
"countryName" : "Spain",
"countryCode" : "ES",
"cityName" : "Madrid",
"latitude" : 40,
"longitude" : -4
},
{
"countryName" : "United Kingdom",
"countryCode" : "GB",
"cityName" : "Soest",
"latitude" : 51.5,
"longitude" : -0.13
}
],
"minute" : 18,
"month" : 11,
"year" : 2013
}
“hourlyLocations”是一系列嵌入式文档(为简洁起见,此处仅显示两个)。
我正在尝试运行一个聚合,它将返回每个国家、该国家的所有城市(一次)以及每个城市的实例数。
这是我到目前为止所得到的:
db.hourly.aggregate(
[
{ "$project" : { "hourly" : "$hourlyLocations" } },
{ "$unwind" : "$hourly" },
{ "$group" : { "_id" : { "country" : "$hourly.countryName" }, "city" : { "$push" : "$hourly.cityName" } } },
]
)
这会返回类似:
{
"_id" : {
"country" : "Italy"
},
"city" : [
"Manzano",
"Cologno Monzese",
"Rome",
"Manzano",
"Cologno Monzese",
"Venice",
"Milan",
"Rome",
"Milan",
"Manzano",
"Cologno Monzese",
"Venice",
"Milan",
"Rome",
"Milan",
"Manzano",
"Cologno Monzese",
"Venice",
"Milan",
"Rome",
"Manzano",
"Cologno Monzese",
"Venice",
"Milan",
"Casalnuovo di Napoli",
"Manzano",
"Cologno Monzese",
"Venice",
"Milan",
"Casalnuovo di Napoli",
"Milan"
]
}
所以我得到了按城市分组的所有城市的所有实例。我现在要做的是按每个城市的实例数进行分组和计数。像这样的东西:
{
"_id" : {
"country" : "Italy"
},
"city" : [
"Casalnuovo di Napoli" : "12"
"Cologno Monzese" : "10",
"Manzano" : "9",
"Milan" : "6",
"Rome" : "3",
"Venice" : "1"
]
}
我已经尝试了几件事,但无法做到正确。如何根据需要获取每个国家/地区的每个城市的计数?
非常感谢,
缺口。