कोशिश करें:
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" } } } }
]
)
यह काफी अनुरोधित संरचना नहीं है। इसके बजाय आपको मिलता है:
{
"_id" : {
"country" : "Italy"
},
"cities" : [
{ "city": "Cologno Monzese", "count": 12},
{ "city": "Milan", "count": 6},
{ "city": "Rome", "count": 3},
]
}