Details
Description
In a sharded cluster, but using an unsharded collection, mongoS, given the query:
mongos.getDB("db").foo.find({a:1}, {a:1}).sort({b:1})
|
Index:{a:1}
|
Will rewrite to:
.find({a:1}, {a:1, b:1}).sort({b:1})
|
This can be observed by running the mongoD at logLevel:2 or greater.
As this is targeting a single shard, the query can run as is, mongoD will return everything in correct sorted order.
Two possible answers:
- This is the correct behavior, we will always rewrite (and accept the cost of adding projections). Better for query plan cache filters.
- This isn't correct behavior and when a single shard is targeted the query shouldn't be rewritten. Better for speed and efficiency.