Aggregation: Question 9 Consider a collection called pianoTuners where every document contains the following fields: { "_id": ObjectId("5592b7d02a205cd86441df0a"), "state": "CA", "city": "San Francisco", "tuner": "Jim's Piano Tuners" } You want to find the total number of piano tuners in each state. Which of the following Aggregation operations will accomplish this? Check all that apply:
题目选项
- A.db.pianoTuners.aggregate([ { $sortByCount: "$state" } ])
- B.db.pianoTuners.aggregate([ { $group: { _id: "$state", numTuners: { $sum: 1 } } } ])
- C.db.pianoTuners.aggregate([ { $count: "state" } ])
答案置信度:高
正确答案:A、B
解析语言:中文
$sortByCount: $state 等价于按 state 分组计数再按计数降序输出;$group 配 $sum: 1 同样能得到每个州的调音师数量。$count 只能输出整个管道的文档总数、不按州分组,所以 C 错。