MongoDB DEV · Question 182 · 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

upsert 的语义是"匹配不到就插入":当没有 a=4 的文档时,MongoDB 把查询条件与 $set 的内容合成一条新文档插入;若匹配到了则只做更新,不会既插又更新(A 错),也不会先删后插(C 错)。还要注意 updateMany+upsert 在无匹配时也最多只插入一条文档。

Papers containing this question

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