MongoDB DBA · 题目 486 · 单选

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:

题目选项

  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 })

答案置信度:高

正确答案:A

解析语言:中文

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

所在套题

此题在题库中有 1 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗