MongoDB DBA · 题目 434 · 单选

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:

题目选项

  1. A.
    { visited: 1, favourites: 1 }
  2. B.
    { favourites: 1, age: 1 }
  3. C.
    { visited: 1, age: 1 }

答案置信度:高

正确答案:C

解析语言:中文

复合索引遵循 ESR 规则:等值字段在前、排序字段居中、范围字段在后。查询中 visited 是等值条件、age 是范围条件,因此 { visited: 1, age: 1 } 既能用等值定位又能顺带完成 age 的范围扫描。{ visited: 1, favourites: 1 } 无法支持 age 过滤,{ favourites: 1, age: 1 } 的最左字段不是查询用到的 visited,两者都只能退化为集合扫描。

所在套题

此题在题库中有 1 条记录。公开详情合并相同题干和选项;完整公开题展示置信度最高的一条记录,其他题目仅展示摘要。按套学习保留每条记录原有的答案、解析和置信度。

作者 · 作品 · 开源

作者与 MongoDB 作品

专注数据平台、MongoDB 迁移同步与云运维产品研发。

有任何 MongoDB 需求,均可联系作者。

联系作者 ↗mlhp123@outlook.com

支持作者

如果题库对您有帮助,欢迎自愿扫码打赏。

支付宝扫码打赏作者打开打赏原图 ↗