4. Consider a collection called sample which contains the following document: { "_id": 3, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 } What will this document look like after running the following updateMany operation? db.sample.updateMany( { "b.c": 4 }, { "$set": { "b.$": { "c": "foo" } } } ) Choose the best answer:
Answer options
- A.{ "_id": 4, "a": { "c": "foo" }, "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 }
- B.{ "_id": 4, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": "foo" } ], "g": 13 }
- C.{ "_id": 3, "a": "tom", "b": { "c": "foo" }, "g": 13 }
- D.{ "_id": 3, "a": { "c": "foo" }, "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": 4, "e": 5 } ], "g": 13 }
- E.{ "_id": 3, "a": "tom", "b": [ { "c": 1 }, { "c": "hello_world", "d": 22 }, { "c": "foo" } ], "g": 13 }
Answer confidence: High
Correct answer: E
Explanation language: Chinese
$set 里的 b.$ 使用位置操作符,会替换 b 数组中第一个满足 b.c: 4 的元素({ c: 4, e: 5 })为 { c: foo },其他元素与 _id 都保持原样,所以是 C。B 把整个数组替换成了子文档;A/D 的 _id 变成 4,而更新操作不会修改 _id。