MongoDB DEV · 题目 200 · 单选

Aggregation: Question 11 Consider a collection called agencies that only contains the following documents: { "_id": ObjectId("565385949a9c0d40e8e6f83f"), "type": "A", "agents": [ "red", "blue", "green" ], "files": [ "a", "a", "b", "b" ] } { "_id": ObjectId("565385c39a9c0d40e8e6f840"), "type": "A", "agents": [ "yellow", "blue" ], "files": [ "b" ] } { "_id": ObjectId("5653862c9a9c0d40e8e6f841"), "type": "A", "agents": [ "red", "green" ], "files": [ "a" ] } What is the result of the following aggregation query? db.agencies.aggregate([ { $unwind: "$agents" }, { $unwind: "$files" }, { $match: { files: "b" } }, { $group: { _id: "$type", count: { $sum: 1 } } } ]) Choose the best answer:

题目选项

  1. A.
    { "_id": "A", "count": 8 }
  2. B.
    { "_id": "A", "count": 3 }
  3. C.
    { "_id": "A", "count": 16 }
  4. D.
    { "_id": "A", "count": 5 }
  5. E.
    { "_id": "A", "count": 2 }

答案置信度:高

正确答案:A

解析语言:中文

先 $unwind agents 再 $unwind files,会把每条文档展开成 agents 数 × files 数条,再筛 files = b:第一条 3 × 4 = 12 条里 b 占一半共 6 条,第二条 2 × 1 = 2 条全是 b,第三条 files 只有 a 被筛掉,合计 8 条,按 type 分组计数即 8,选 A。

所在套题

此题在题库中有 3 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗