MongoDB DBA · Question 487 · Single choice

4. Consider the following document: { "_id": 12, "a": [] } Which of the following commands will append the value 5 to the array in the a field? Choose the best answer:

Answer options

  1. A.
    db.sample.updateOne({ "_id": 12 }, { $pop: { "a": 5 } })
  2. B.
    db.sample.updateOne({ $push: { "a": 5 } }, { "_id": 12 })
  3. C.
    db.sample.updateOne({ "_id": 12 }, { $push: { "a": 5 } })
  4. D.
    db.sample.updateOne({ "_id": 12, "a": { $exists: true } }, { $push: { "a": 5 } })

Answer confidence: Low

Correct answer: D

Explanation language: Chinese

过滤条件 {_id:12, a:{$exists:true}} 能匹配 a 为空数组的文档(字段存在即匹配),$push 会把 5 追加到该数组,因此 A 可以达到目的。但本题选项存在歧义:D 的 {_id:12} 同样能匹配并完成追加,两种写法结果完全一致,题目却要求单选;源卷把答案记为 A,故按 A 判定并降低置信度。

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 ↗