You have created the following index on a collection called people: db.people.createIndex({ lastName : 1, firstName : 1, birthDate : -1 }) Which of the following queries can use the above index for sorting? Check all that apply:
题目选项
- A.db.people.find().sort({ birthDate : -1 })
- B.db.people.find().sort({ lastName : -1, firstName : -1, birthDate : -1 })
- C.db.people.find({ lastName : "Jones" } ).sort({ firstName : 1 })
答案置信度:高
正确答案:C
解析语言:中文
该索引正向顺序是 (lastName 升, firstName 升, birthDate 降),反向扫描则是 (lastName 降, firstName 降, birthDate 升)。A 在 lastName 等值后按 firstName:1 排序,方向与索引前缀一致,可用索引;B 缺首字段 lastName,无法用索引排序;C 三字段全降序既不等于索引方向,也不是它的"完全反向"(完全反向时 birthDate 应为升序),只能走内存排序。