|
Hi tomgrossman,
The behavior that you describe is expected and is documented here. I will present a simple example with two fields to try and give you an idea of what is going on.
Please consider the following collection sorted on the compound index {a:1,b:1}:
db.foo.find().sort({a:1,b:1})
|
{ "_id" : ObjectId("5703cbf1d30acd083f35143e"), "a" : 1, "b" : 1 }
|
{ "_id" : ObjectId("5703cbf4d30acd083f35143f"), "a" : 1, "b" : 2 }
|
{ "_id" : ObjectId("5703cbf7d30acd083f351440"), "a" : 2, "b" : 1 }
|
{ "_id" : ObjectId("5703cbf9d30acd083f351441"), "a" : 2, "b" : 2 }
|
These documents are not sorted by b, and so sorting on b would not utilize this index.
However, if we add an equality match on a, we can see that the results will be sorted by b:
db.foo.find({a:1}).sort({a:1,b:1})
|
{ "_id" : ObjectId("5703cbf1d30acd083f35143e"), "a" : 1, "b" : 1 }
|
{ "_id" : ObjectId("5703cbf4d30acd083f35143f"), "a" : 1, "b" : 2 }
|
Consequently, the compound index {a:1,b:1} cannot be used to sort
db.foo.find({}).sort({b:1})
|
but can sort
|
db.foo.find({a:1}).sort({b:1})
|
I hope this clarifies this behavior.
Kind regards,
Thomas
|