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:
题目选项
- A.db.products.find( { "price": 200 } ) .sort( { "numReviews": 1, "rating": -1 } )
- B.db.products.find( { "price": 200 } ) .sort( { "rating": 1 } )
- C.db.products.find( { "price": 100 } ) .sort( { "numReviews": -1, "rating": -1 } )
答案置信度:高
正确答案:B
解析语言:中文
复合索引 {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 的位置,两者都只能走内存排序。