-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 2.0.45
-
Component/s: None
-
Empty show more show less
Order of sort fields is lost for numeric field names. Consider this example:
{{collection.find({},
{ sort: [['2', 1], ['1', 1]] })}}
The expected result would be to first sort on field 2, then sort on field 1. Instead, it is first sorted on field 1, then on field 2.
The reason is that the driver converts the array of sort fields into an object in the method formattedOrderClause in utils.js (called at http://mongodb.github.io/node-mongodb-native/2.0/api/lib_collection.js.html#line346). Here the order is lost, because numeric property names are not necessarily enumerated in insertion order:
ECMA-262 does not specify enumeration order. The de facto standard is to match
insertion order, which V8 also does, but with one exception:V8 gives no guarantees on the enumeration order for array indices (i.e., a property
name that can be parsed as a 32-bit unsigned integer).
- is related to
-
NODE-3173 sort behaves differently when passed as an option or called as a method on cursor
- Closed
-
NODE-3174 Driver 4 doesn't preserve the sort key order
- Closed
-
MONGOSH-1037 Numeric field are sorted to the front (js shell)
- Closed
- related to
-
COMPASS-4258 Sort does not preserve field order
- Closed