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:
题目选项
- A.db.sample.updateOne({ "_id": 12 }, { $pop: { "a": 5 } })
- B.db.sample.updateOne({ $push: { "a": 5 } }, { "_id": 12 })
- C.db.sample.updateOne({ "_id": 12 }, { $push: { "a": 5 } })
- D.db.sample.updateOne({ "_id": 12, "a": { $exists: true } }, { $push: { "a": 5 } })
答案置信度:低
正确答案:D
解析语言:中文
过滤条件 {_id:12, a:{$exists:true}} 能匹配 a 为空数组的文档(字段存在即匹配),$push 会把 5 追加到该数组,因此 A 可以达到目的。但本题选项存在歧义:D 的 {_id:12} 同样能匹配并完成追加,两种写法结果完全一致,题目却要求单选;源卷把答案记为 A,故按 A 判定并降低置信度。