ISSUE SUMMARY
This ticket describes two issues with natural order sorts on indexed queries.
1. If a query over an indexed field is specified, the natural sort order is ignored.
Example:
Assume an index on {x: 1}.
db.coll.find( {x: "some value"} ).sort( {$natural: 1} )
|
This example ignores the natural sort order and instead returns results ordered according to the index scan.
2. If both a hint on $natural and a sort on $natural are specified, then the hint direction overrules the sort direction. This is a deviation from the 2.4 behavior, where .sort() overruled .hint().
Example:
db.coll.find( {x: "some value"} ).hint( {$natural: 1} ).sort( {$natural: -1} )
|
This example sorts in ascending natural order (1) instead of descending (-1).
USER IMPACT
Documents returned by such queries may not be in the sort order the user expects.
WORKAROUNDS
In both cases, specifying only a .hint() and not a .sort() will work around the issue.
AFFECTED VERSIONS
Versions 2.6.0 and 2.6.1 are affected by this issue.
FIX VERSION
The fix is included in the 2.6.2 production release.
RESOLUTION DETAILS
The query planner now obeys the $natural sort order and prefers .sort() to .hint() if both are specified. This restores the behavior of version 2.4.