MongoDB DEV · 题目 80 · 单选

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:

题目选项

  1. A.
    There will be no documents in the result set
  2. 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" }
  3. 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 ] }
  4. D.
    { "_id": { "city": [ "Albany"], "state": [ "NY" ] }, "county": [ "Albany" ], "zips": [ 12224 ] } } { "_id": { "city": [ "New York" ], "state": [ "NY" ] }, "county": [ "Kings", "New York" ], "zips": [ 10036, 11211, 10001 ] } }
  5. 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 语义。

所在套题

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

作者 · 作品 · 开源

作者与 MongoDB 作品

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

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

联系作者 ↗mlhp123@outlook.com

支持作者

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

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