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:
题目选项
- A.db.sample.updateOne({ "_id": 12 }, { "$unset": { "c": "" } })
- B.db.sample.updateOne({ "_id": 12 }, { "c": { "$delete": true } })
- 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 },字段并没有被删掉。