首页 > 解决方案 > 如何在字段 MongoDB 中找到按单词搜索的用户的平均年龄?

问题描述

我会找到"users"来自阿拉斯加的平均年龄。

db.users.aggregate({ $match: { address:  "Alaska" } }, { $group: { _id: null, averageAge: { $avg: '$age'} } })

用户示例:

{
            "_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.",
}

标签: mongodbshellsearchaggregate

解决方案


推荐阅读