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 result returned by a query, which of the choices below is the query that would generate this result? { _id: 3, a: [ 6, 7 ] } Check all that apply:
题目选项
- A.db.coll.find( { a: { $all: [ 4, 7 ] } } )
- B.db.coll.find( { a: { $all: [ 7, 6 ] } } )
- C.db.coll.find( { a: { $elemMatch: { $gt: 3, $lt: 6 } } } )
答案置信度:高
正确答案:B
解析语言:中文
$all [7,6] 要求数组同时包含 6 和 7(顺序无关),只有 {_id:3, a:[6,7]} 满足,结果正好是题目给出的那一个文档。迷惑项 A 的 $elemMatch {$gt:3,$lt:6} 要求同一个元素既大于 3 又小于 6,会命中 {_id:5,a:[4,4]};B 的 $all [4,7] 要求同时含 4 和 7,没有文档满足。