MongoDB DEV · Question 198 · Multiple choice

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:

Answer options

  1. A.
    db.pianoTuners.aggregate([ { $sortByCount: "$state" } ])
  2. B.
    db.pianoTuners.aggregate([ { $group: { _id: "$state", numTuners: { $sum: 1 } } } ])
  3. C.
    db.pianoTuners.aggregate([ { $count: "state" } ])

Answer confidence: High

Correct answer: A, B

Explanation language: Chinese

$sortByCount: $state 等价于按 state 分组计数再按计数降序输出;$group 配 $sum: 1 同样能得到每个州的调音师数量。$count 只能输出整个管道的文档总数、不按州分组,所以 C 错。

Papers containing this question

This question appears in 3 bank records. Public pages combine identical prompts and options. Selected questions display the highest-confidence record; the others show a short preview. Paper-based learning preserves each record's answer, explanation, and confidence.

AUTHOR · PROJECTS · OPEN SOURCE

Author & MongoDB projects

Building data platforms, MongoDB migration and sync tools, and cloud operations products.

Contact the author for any MongoDB needs.

Contact the author ↗mlhp123@outlook.com

Support the author

If this question bank helps you, optional donations are welcome.

Scan with Alipay to support the authorOpen full-size donation image ↗