Consider a collection called coll which contains only the following documents: { _id: 2, a: [ 3, 7 ] } { _id: 3, a: [ 6, 7 ] } { _id: 5, a: [ 4, 4 ] } The following is the desired update to the document, which of the choices below would generate this update to the document? { _id: 5, a: [ 4, 4, 7 ] } Check all that apply:
题目选项
- A.db.coll.update( { a: 4 }, { $push: { a: 4 } }
- B.db.coll.update( { a: 3 }, { $push: { a: 4 } } )
- C.db.coll.update( { a: 4 }, { $push: { a: 7 } } )
答案置信度:高
正确答案:C
解析语言:中文
目标结果 {_id:5, a:[4,4,7]} 只能由 _id:5 的文档(a:[4,4])再 push 一个 7 得到:C 用 {a:4} 定位到该文档后 $push:7,正确。A 命中同一个文档但 push 的是 4,结果是 [4,4,4];B 的 {a:3} 匹配的是 _id:2(a:[3,7]),会把 4 加到错误的文档上。(A 选项缺一个右括号,但不影响判断。)