MongoDB DBA · 题目 426 · 单选

4. Consider a collection called sample which contains the following document: { "_id": 3, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 } What will this document look like after running the following updateMany operation? db.sample.updateMany( { "b.c": 4 }, { "$set": { "b.$": { "c": "foo" } } } ) Choose the best answer:

题目选项

  1. A.
    { "_id": 4, "a": { "c": "foo" }, "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 }
  2. B.
    { "_id": 4, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": "foo" } ], "g": 13 }
  3. C.
    { "_id": 3, "a": "tom", "b": { "c": "foo" }, "g": 13 }
  4. D.
    { "_id": 3, "a": { "c": "foo" }, "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 }
  5. E.
    { "_id": 3, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": "foo" } ], "g": 13 }

答案置信度:高

正确答案:E

解析语言:中文

$set 里的 b.$ 使用位置操作符,会替换 b 数组中第一个满足 b.c: 4 的元素({ c: 4, e: 5 })为 { c: foo },其他元素与 _id 都保持原样,所以是 C。B 把整个数组替换成了子文档;A/D 的 _id 变成 4,而更新操作不会修改 _id。

所在套题

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

作者 · 作品 · 开源

作者与 MongoDB 作品

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

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

联系作者 ↗mlhp123@outlook.com

支持作者

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

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