我有一个看起来像这样的文件:
{
"_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
}
Run Code Online (Sandbox Code Playgroud)
"hourlyLocations"是一系列嵌入式文档(为简洁起见,这里只显示了两个).
我正在尝试运行一个聚合,它将返回每个国家,该国家的所有城市(一次)和每个城市的实例数量.
这是我到目前为止所得到的:
db.hourly.aggregate(
[
{ "$project" : { "hourly" : "$hourlyLocations" } },
{ "$unwind" : "$hourly" },
{ …Run Code Online (Sandbox Code Playgroud) 我有以下查询:
db.listener.aggregate(
[
{ "$match" : { "location.countryName" : "Italy" } },
{ "$project" : { "location" : "$location"} },
{ "$group" : { "_id" : { "country": "$location.countryName", "city": "$location.cityName" }, "count" : { "$sum" : 1 }, "co-ords" : { "$addToSet" : { "lat" : "$location.latitude", "long" : "$location.longitude" } } } },
{ "$group" : { "_id" : "$_id.country", "cities" : { "$push" : { "city" : "$_id.city", "count" : "$count", "co-ords" : "$co-ords" } } } …Run Code Online (Sandbox Code Playgroud)