我会发现阿拉斯加的平均年龄为"users"
。
db.users.aggregate({ $match: { address: "Alaska" } }, { $group: { _id: null, averageAge: { $avg: '$age'} } })
'address'
是用户的完整地址。用户示例:
{
"_id" : ObjectId("5adf3c1544abaca147cdd34b"),
"index" : 2,
"guid" : "bd579f65-83a2-4966-8e33-da078369eb15",
"isActive" : false,
"balance" : "$3,118.30",
"picture" : "http://placehold.it/32x32",
"age" : 40,
"eyeColor" : "blue",
"name" : "Macias Shannon",
"gender" : "male",
"company" : "NORSUL",
"email" : "maciasshannon@norsul.com",
"phone" : "+1 (846) 566-2908",
"address" : "221 Gardner Avenue, Tetherow, Virginia, 464",
"about" : "Cupidatat ad esse ad nulla aute eiusmod veniam minim esse labore id et ipsum.\r\n",
"greeting" : "Hello, Macias Shannon! You have 1 unread messages.",
}