MongoDB DBA · Question 491 · Multiple choice

8. Consider the following index in the products collection: { price: 1, rating: 1, numReviews: 1 } Which of the following queries could use this index for filtering and sorting? Check all that apply:

Answer options

  1. A.
    db.products.find( { "price": 200 } ) .sort( { "numReviews": 1, "rating": -1 } )
  2. B.
    db.products.find( { "price": 200 } ) .sort( { "rating": 1 } )
  3. C.
    db.products.find( { "price": 100 } ) .sort( { "numReviews": -1, "rating": -1 } )

Answer confidence: High

Correct answer: B

Explanation language: Chinese

复合索引 {price:1, rating:1, numReviews:1} 里,price 有等值条件后,排序字段必须按索引顺序连续出现:B 的 {price:200} 加 sort({rating:1}) 正好用 rating 作为紧随其后的排序字段,可完全由索引完成。A 的 sort({numReviews:1, rating:-1}) 字段顺序与索引相反且方向混杂,C 的 sort({numReviews:-1, rating:-1}) 跳过了 rating 的位置,两者都只能走内存排序。

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 ↗