MongoDB DBA · Question 426 · Single choice

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:

Answer options

  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 }

Answer confidence: High

Correct answer: E

Explanation language: Chinese

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

Papers containing this question

This question appears in 1 bank records. Public pages combine identical prompts and options. Selected questions display the highest-confidence record; the others show a short preview. Paper-based learning preserves each record's answer, explanation, and confidence.

AUTHOR · PROJECTS · OPEN SOURCE

Author & MongoDB projects

Building data platforms, MongoDB migration and sync tools, and cloud operations products.

Contact the author for any MongoDB needs.

Contact the author ↗mlhp123@outlook.com

Support the author

If this question bank helps you, optional donations are welcome.

Scan with Alipay to support the authorOpen full-size donation image ↗