-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 4.0.0
-
Component/s: None
-
Empty show more show less
-
Not Needed
Using array as a sort value to preserve sort order for numeric keys doesn't work. To reproduce you can use steps similar to NODE-3173:
Create a small collection with a few numeric keys:
> await client.db('test').collection('coll').insertMany([{ '1': 1 }, { '2': 1 }, { '3': 1 }])
And then try to sort the output using sort method or a sort option passed to find method:
> await client.db('test').collection('coll2').find().sort([['3', -1], ['1', -1]]).toArray() [ { '1': 1, _id: new ObjectId("605cacc4a5b7d77dce3b4b23") }, { '3': 1, _id: new ObjectId("605cacd1a5b7d77dce3b4b25") }, { '2': 1, _id: new ObjectId("605cacc8a5b7d77dce3b4b24") } ] > await client.db('test').collection('coll2').find({}, {sort: [['3', -1], ['1', -1]]}).toArray() [ { '1': 1, _id: new ObjectId("605cacc4a5b7d77dce3b4b23") }, { '3': 1, _id: new ObjectId("605cacd1a5b7d77dce3b4b25") }, { '2': 1, _id: new ObjectId("605cacc8a5b7d77dce3b4b24") } ]
Both outputs are the same, but not expected. The expected result would be:
[ { '3': 1, _id: 605cacd1a5b7d77dce3b4b25 }, { '1': 1, _id: 605cacc4a5b7d77dce3b4b23 }, { '2': 1, _id: 605cacc8a5b7d77dce3b4b24 } ]