Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-35477

Sort by array field doesn't work

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • None
    • ALL

      First case
      Running MongoDB 3.6.5 and preparing collection:

      # mongo
      MongoDB shell version v3.6.5
      connecting to: mongodb://127.0.0.1:27017
      MongoDB server version: 3.6.5
      > use test
      switched to db test
      > db.test.drop()
      true
      > db.test.insert({k: [1, 'x']})
      WriteResult({ "nInserted" : 1 })
      > db.test.insert({k: [1, 'y']})
      WriteResult({ "nInserted" : 1 })
      > db.test.insert({k: [1, 'x']})
      WriteResult({ "nInserted" : 1 })

       
      Making asc sort and getting wrong result:

      > db.test.aggregate([{$sort: {k: 1}}])
      { "_id" : ObjectId("5b191e295069772f72806a1c"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b191e2b5069772f72806a1d"), "k" : [ 1, "y" ] }
      { "_id" : ObjectId("5b191e2e5069772f72806a1e"), "k" : [ 1, "x" ] }
      

       
      Expected:

      { "_id" : ObjectId("5b191e295069772f72806a1c"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b191e2e5069772f72806a1e"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b191e2b5069772f72806a1d"), "k" : [ 1, "y" ] }
      

      Meanwhile desc sort works well:

      > db.test.aggregate([{$sort: {k: -1}}])
      { "_id" : ObjectId("5b191e2b5069772f72806a1d"), "k" : [ 1, "y" ] }
      { "_id" : ObjectId("5b191e295069772f72806a1c"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b191e2e5069772f72806a1e"), "k" : [ 1, "x" ] }
      

      Second case
      In MongoDB 3.4.6 things work slightly different.

      Cursor sort asc works wrong:

      > db.test.find().sort({k: 1})
      { "_id" : ObjectId("5b19203af8c333c3a244ac98"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b19203df8c333c3a244ac99"), "k" : [ 1, "y" ] }
      { "_id" : ObjectId("5b192040f8c333c3a244ac9a"), "k" : [ 1, "x" ] }
      

      Cursor sort desc works right:

      > db.test.find().sort({k: -1})
      { "_id" : ObjectId("5b19203df8c333c3a244ac99"), "k" : [ 1, "y" ] }
      { "_id" : ObjectId("5b19203af8c333c3a244ac98"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b192040f8c333c3a244ac9a"), "k" : [ 1, "x" ] }
      

      Aggregation sort asc works right:

      > db.test.aggregate([{$sort: {k: 1}}])
      { "_id" : ObjectId("5b19203af8c333c3a244ac98"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b192040f8c333c3a244ac9a"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b19203df8c333c3a244ac99"), "k" : [ 1, "y" ] }
      

      Aggregation sort desc works right:

      > db.test.aggregate([{$sort: {k: -1}}])
      { "_id" : ObjectId("5b19203df8c333c3a244ac99"), "k" : [ 1, "y" ] }
      { "_id" : ObjectId("5b19203af8c333c3a244ac98"), "k" : [ 1, "x" ] }
      { "_id" : ObjectId("5b192040f8c333c3a244ac9a"), "k" : [ 1, "x" ] }
      

       

            Assignee:
            nick.brewer Nick Brewer
            Reporter:
            ayamnikov ayamnikov
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: