MongoDB DBA · Question 486 · Single choice

3. Consider a collection called sample where every document contains the following fields: { "_id": 3, "a": 7, "c": [ 3, 4, 7, -1, -1 ] } Which of the following operations will remove the "c" field from the document with "_id": 12? Choose the best answer:

Answer options

  1. A.
    db.sample.updateOne({ "_id": 12 }, { "$unset": { "c": "" } })
  2. B.
    db.sample.updateOne({ "_id": 12 }, { "c": { "$delete": true } })
  3. C.
    db.sample.updateOne({ "_id": 12 }, { "c": null })

Answer confidence: High

Correct answer: A

Explanation language: Chinese

$unset 的作用是删除字段,其值会被忽略,所以 { $unset: { "c": "" } } 能正确移除 c 字段(A)。B 的 $delete 不是 MongoDB 的更新运算符(删除字段只能用 $unset)。C 的 { "c": null } 不含任何更新运算符,会被当作整体替换文档处理,结果是把文档替换成 { _id: 12, c: null },字段并没有被删掉。

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 ↗