Consider a collection called numbers where the following updateOne is run: db.numbers.updateOne( { a: 4 }, { $set: { b: 2, d: -4 } } ) Consider the document prior to updating had these values: { "_id": ObjectId("57fd48257268886f789b33fa"), "a": 4, "b": 1, "c": 3, "d": 8 } What is the expected resulting document after the update query above ? Choose the best answer:
题目选项
- A.{ "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 2, "c": 3, "d": -4 }
- B.{ "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 1, "c": 3, "d": -4 }
- C.{ "_id": ObjectId("57fd48257268886f789b33ff"), "a": 4, "b": 2, "c": 3, "d": 4 }
答案置信度:中
正确答案:A
解析语言:中文
$set 是“存在则覆盖、不存在则新增”,所以 b 由 1 改为 2、d 由 8 改为 -4,而未被 $set 提及的 a、c 保持不变,结果为 {a:4,b:2,c:3,d:-4}(A)。B 的 b 仍是 1,C 的 d 变成 4($set 是赋值不是相加),都不对。注:三个选项里的 _id 末尾与题干文档不一致,属于题干转录笔误,不影响字段取值的判断。