MongoDB DBA · Question 546 · Single choice

Given the following query: db.books.find( { "category": "kids books", "price": { "$lt": 20.00 } } ).sort( { "number_sold": -1 } ) Which index on the books collection will be the most performant for the above query? Choose the best answer:

Answer options

  1. A.
    {"number_sold": 1, "category": 1, "price": 1}
  2. B.
    {"price": 1, "number_sold": 1, "category": 1}
  3. C.
    {"category": 1, "price": 1, "number_sold": 1}
  4. D.
    {"number_sold": 1, "price": 1, "category": 1}
  5. E.
    {"category": 1, "number_sold": 1, "price": 1}

Answer confidence: High

Correct answer: E

Explanation language: Chinese

按 ESR 规则(等值 → 排序 → 范围)排列字段:category 是等值条件、number_sold 是排序字段、price 是范围条件,所以最优索引是 { category: 1, number_sold: 1, price: 1 }。C 把 price 放在排序字段之前,索引无法支撑排序;A、D 以 number_sold 开头,无法用 category 做前缀等值定位、只能全索引扫描;B 同样用不上 category 的等值匹配。单字段排序可由索引反向扫描实现,所以 number_sold: 1 也能满足 sort: -1。

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 ↗