Consider a collection places that only contains the following documents: { "_id": 0, "city": "New York", "state": "NY", "zip": 10001, "county": "New York" } { "_id": 1, "city": "New York", "state": "NY", "zip": 11211, "county": "Kings" } { "_id": 2, "city": "New York", "state": "NY", "zip": 10036, "county": "New York" } { "_id": 3, "city": "Albany", "state": "NY", "zip": 12224, "county": "Albany" } You perform the following aggregation operation against the places collection: db.places.aggregate([ { $group: { _id: { city: "$city", state: "$state" }, county: { $addToSet: "$county" }, zips: { $addToSet: "$zip" } } } ]) Which of the following represents the output of this operation? Choose the best answer:
题目选项
- A.There will be no documents in the result set
- B.{ "_id": 0, "city": "New York", "state": "NY", "zip": 10001, "county": "New York" } { "_id": 1, "city": "New York", "state": "NY", "zip": 11211, "county": "Kings" } { "_id": 2, "city": "New York", "state": "NY", "zip": 10036, "county": "New York" } { "_id": 3, "city": "Albany", "state": "NY", "zip": 12224, "county": "Albany" }
- C.{ "_id": { "city": [ "Albany"], "state": [ "NY" ] }, "county": [ "Albany" ], "zips": [ 12224 ] } { "_id": { "city": [ "New York", "New York", "New York" ], "state": [ "NY", "NY", "NY" ] }, "county": [ "Kings", "New York" ], "zips": [ 10036, 11211, 10001 ] }
- D.{ "_id": { "city": [ "Albany"], "state": [ "NY" ] }, "county": [ "Albany" ], "zips": [ 12224 ] } } { "_id": { "city": [ "New York" ], "state": [ "NY" ] }, "county": [ "Kings", "New York" ], "zips": [ 10036, 11211, 10001 ] } }
- E.{ "_id": { "city": "Albany", "state": "NY" }, "county": [ "Albany" ], "zips": [ 12224 ] } { "_id": { "city": "New York", "state": "NY" }, "county": [ "Kings", "New York" ], "zips": [ 10036, 11211, 10001 ] }
答案置信度:高
正确答案:E
解析语言:中文
$group 的 _id 是 {city:"$city", state:"$state"} 这样的文档,因此只会产生 Albany/NY 与 New York/NY 两组,county 与 zip 用 $addToSet 去重成数组,输出形如 E。A 的空结果不对;B 是原始文档(没有被分组);C、D 把 city/state 写成数组并出现了不存在的分组,都不符合 $group 语义。