MongoDB DEV · Question 552 · Multiple choice

How would the following query change if we set { upsert: true } as the third parameter? db.sample.updateMany( { a: 4 }, { $set: { b: 2, d: -4 } } ) Check all that apply:

Answer options

  1. A.
    The upsert will both insert and update a document.
  2. B.
    The upsert will insert a document if no documents exist where "a" is equal to 4
  3. C.
    An upsert will remove a document, if found, before inserting a new document.

Answer confidence: High

Correct answer: B

Explanation language: Chinese

updateMany 加 upsert: true 的语义是:没有文档匹配 { a: 4 } 时插入一条新文档(插入内容由查询条件与 $set 共同构成),有匹配时只做更新,绝不会“既更新又插入”(A 错)。upsert 不会删除任何文档,因此 C 也错。

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 ↗