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:
题目选项
- A.{ "_id": "A", "count": 8 }
- B.{ "_id": "A", "count": 3 }
- C.{ "_id": "A", "count": 16 }
- D.{ "_id": "A", "count": 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。