Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-3174

Driver 4 doesn't preserve the sort key order

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 4.0.0
    • Affects Version/s: 4.0.0
    • Component/s: None
    • 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 }
      ]
      

            Assignee:
            daria.pardue@mongodb.com Daria Pardue
            Reporter:
            sergey.petushkov@mongodb.com Sergey Petushkov
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: