MongoDB DBA · Question 434 · Single choice

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

  1. A.
    { visited: 1, favourites: 1 }
  2. B.
    { favourites: 1, age: 1 }
  3. 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,两者都只能退化为集合扫描。

Papers containing this question

This question appears in 1 bank records. Public pages combine identical prompts and options. Selected questions display the highest-confidence record; the others show a short preview. Paper-based learning preserves each record's answer, explanation, and confidence.

AUTHOR · PROJECTS · OPEN SOURCE

Author & MongoDB projects

Building data platforms, MongoDB migration and sync tools, and cloud operations products.

Contact the author for any MongoDB needs.

Contact the author ↗mlhp123@outlook.com

Support the author

If this question bank helps you, optional donations are welcome.

Scan with Alipay to support the authorOpen full-size donation image ↗