Zkuste:
db.hourly.aggregate(
[
{ "$project" : { "hourly" : "$hourlyLocations" } },
{ "$unwind" : "$hourly" },
{ $group: { _id: { country: "$hourly.countryName", city: "$hourly.cityName" }, count: { $sum: 1 } } },
{ $sort: { count: -1 } },
{ $group: { _id: "$_id.country", cities: { $push: { city: "$_id.city", count: "$count" } } } }
]
)
Není to úplně požadovaná struktura. Místo toho dostanete:
{
"_id" : {
"country" : "Italy"
},
"cities" : [
{ "city": "Cologno Monzese", "count": 12},
{ "city": "Milan", "count": 6},
{ "city": "Rome", "count": 3},
]
}