MongoDB DBA · 题目 328 · 多选

47. You have created the following index on the travel collection: db.travel.createIndex( { "name" : 1, "age" : -1, "visited" : 1 } ) Which of the following queries will be able to fulfill the query without an in-memory sort (i.e., it's able to use the index to sort)? Check all that apply:

题目选项

  1. A.
    db.travel.find( {"name": "Lillian Hernandez", "visited" : /^I/ } ) .sort( { "age": 1} )
  2. B.
    db.travel.find( { "age" : { "$gt": 22 } } ) .sort( { "visited": -1 } )
  3. C.
    db.travel.find( { "name": "Randy Salazar" } ) .sort( { "age": -1 } )

答案置信度:中

正确答案:A、C

解析语言:中文

C 在 name 上有等值条件、按 age:-1 排序,正好与索引 {name:1, age:-1, visited:1} 的 (name, age) 前缀方向一致,可直接用索引出序。A 同样在 name 上有等值条件,按 age:1 排序可用索引反向扫描得到 age 升序(visited 的正则只是过滤条件,不影响排序)。B 在 age 上是范围条件,其后的 visited 在索引里不再有序,必须回内存排序。

所在套题

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

作者 · 作品 · 开源

作者与 MongoDB 作品

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

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

联系作者 ↗mlhp123@outlook.com

支持作者

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

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