11. Consider a collection called travel with the following index: { "name": 1,"age": -1,"visited": 1 } Which of the following queries can use this index? Check all that apply:
题目选项
- A.db.travel.find( { "visited.0": "Ireland"} )
- B.db.travel.find( { "age": { "$lt": 10} } ).sort( { "name": 1 } )
- C.db.travel.find( { } ).sort( { "name": 1 } )
- D.db.travel.find( { "age": { "$lt": 40 }, "visited": "Monaco" } )
- E.db.travel.find( { "visited": "Ireland", "name": "Lillian Hernandez" } )
答案置信度:中
正确答案:B、C、E
解析语言:中文
B、C 都是靠索引提供排序:索引首字段是 name,查询虽然过滤 age,但 sort({name:1}) 是索引前缀,MongoDB 会做整索引扫描(IXSCAN)后 FETCH 再过滤 age,从而避免内存排序。E 对首字段 name 的等值条件足以使用索引(visited 作为残余过滤),所以也可用。A 错在 "visited.0" 是数组位置子路径,而索引建在 visited 整体上、键是元素值,两者对不上;D 跳过了首字段 name,age/visited 都落在非前缀位置,构不成有界索引扫描。