假设我有一个这样的集合:
{
_id: 1,
city: "New York",
state: "NY",
murders: 328
}
{
_id: 2,
city: "Los Angeles",
state: "CA",
murders: 328
}
...
该系列向我们展示了美国所有城市的谋杀案数量。我想计算全国所有谋杀案的平均数。我试着用
$group:
db.murders.aggregate([{$group: {_id:"$state", pop: {$avg:"$murders"} } }])
但是我得到了州政府谋杀案的结果:
{ "_id" : "NY", "murders" : 200 }
{ "_id" : "NJ", "murders" : 150 }
{ "_id" : "CA", "murders" : 230 }
{ "_id" : "CT", "murders" : 120 }
我的问题是,如何将此结果分组以计算独特的平均值?像这样:
{ "_id" : "USA", "murders" : 175 }
答案 0 :(得分:11)