MongoDB DEV · Question 183 · Single choice

Consider a collection called numbers where the following updateOne is run: db.numbers.updateOne( { a: 4 }, { $set: { b: 2, d: -4 } } ) Consider the document prior to updating had these values: { "_id": ObjectId("57fd48257268886f789b33fa"), "a": 4, "b": 1, "c": 3, "d": 8 } What is the expected resulting document after the update query above ? Choose the best answer:

Answer options

  1. A.
    { "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 2, "c": 3, "d": -4 }
  2. B.
    { "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 1, "c": 3, "d": -4 }
  3. C.
    { "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 2, "c": 3, "d": 4 }

Answer confidence: Medium

Correct answer: A

Explanation language: Chinese

$set 是“存在则覆盖、不存在则新增”,所以 b 由 1 改为 2、d 由 8 改为 -4,而未被 $set 提及的 a、c 保持不变,结果为 {a:4,b:2,c:3,d:-4}(A)。B 的 b 仍是 1,C 的 d 变成 4($set 是赋值不是相加),都不对。注:三个选项里的 _id 末尾与题干文档不一致,属于题干转录笔误,不影响字段取值的判断。

Papers containing this question

This question appears in 3 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 ↗