47. You have created the following index on the travel collection: db.travel.createIndex( { "name" : 1, "age" : -1, "visited" : 1 } ) Which of the following queries will be able to fulfill the query without an in-memory sort (i.e., it's able to use the index to sort)? Check all that apply:
Answer options
- A.db.travel.find( {"name": "Lillian Hernandez", "visited" : /^I/ } ) .sort( { "age": 1} )
- B.db.travel.find( { "age" : { "$gt": 22 } } ) .sort( { "visited": -1 } )
- C.db.travel.find( { "name": "Randy Salazar" } ) .sort( { "age": -1 } )
Answer confidence: Medium
Correct answer: A, C
Explanation language: Chinese
C 在 name 上有等值条件、按 age:-1 排序,正好与索引 {name:1, age:-1, visited:1} 的 (name, age) 前缀方向一致,可直接用索引出序。A 同样在 name 上有等值条件,按 age:1 排序可用索引反向扫描得到 age 升序(visited 的正则只是过滤条件,不影响排序)。B 在 age 上是范围条件,其后的 visited 在索引里不再有序,必须回内存排序。