9. Consider the collection called travel that contains only the following 2 documents: { "_id" : ObjectId("5ab0615f5b3e785dd5fa3705"), "name" : "Randy Salazar", "age" : 60, "visited" : [ "Israel", "São Tomé and Príncipe", "Malta", "Ethiopia" ], "favourites" : [ "São Tomé and Príncipe", "Malta" ] } { "_id" : ObjectId("5ab0615f5b3e785dd5fa3706"), "name" : "Lillian Hernandez", "age" : 38, "visited" : [ "Canary Islands" ], "favourites" : [ "Canary Islands" ] } Consider the following potential indexes on that travel collection: { visited: 1, age: 1 } { visited: 1, favourites: 1} { favourites: 1, age: 1 } Given the following query below, select which of these three indexes is the best overall to support the query. db.travel.find({ "age": { "$gt": 35 }, "visited": "Malta" }) Choose the best answer:
Answer options
- A.{ visited: 1, favourites: 1 }
- B.{ favourites: 1, age: 1 }
- C.{ visited: 1, age: 1 }
Answer confidence: High
Correct answer: C
Explanation language: Chinese
复合索引遵循 ESR 规则:等值字段在前、排序字段居中、范围字段在后。查询中 visited 是等值条件、age 是范围条件,因此 { visited: 1, age: 1 } 既能用等值定位又能顺带完成 age 的范围扫描。{ visited: 1, favourites: 1 } 无法支持 age 过滤,{ favourites: 1, age: 1 } 的最左字段不是查询用到的 visited,两者都只能退化为集合扫描。