非常感谢,您的回答很有帮助。
我在本地集合中做了一些测试,确实排序部分需要被索引覆盖才能有一个被覆盖的查询。
示例:
1 .- 我在一个空集合中创建了一个索引:
db.myCollection.createIndex({x:1,y:1,z:1})
2 .- 我已插入数据
db.myCollection.insert({x:5})
3.- 我做了不同的查询并使用“解释”来查看查询是否被覆盖(indexOnly:true)
db.myCollection.find({x:5},{x:1,_id:0}) --> it is covered !
db.myCollection.find({x:5},{x:1,_id:0}).sort({x:1}) --> it is covered !
db.myCollection.find({x:5,y:4},{x:1,_id:0}).sort({x:-1,y:-1}) --> it is covered!
db.myCollection.find({x:5},{x:1,_id:0}).sort({z:1}) --> it is NOT covered !
db.myCollection.find({x:5},{x:1,_id:0}).sort({y:-1,x:-1}) --> it is NOT covered !
所以这个例子表明,“排序”会影响查询是覆盖查询这一事实