Uploaded image for project: 'Documentation'
  1. Documentation
  2. DOCS-11978

The documented BSON types sort order differs from the actual one

    XMLWordPrintableJSON

Details

    Description

      ----------------------------

      Original Description

      The comparison/sort order documentation (https://docs.mongodb.com/manual/reference/bson-type-comparison-order/#bson-types-comparison-order) provides the following sort order of BSON types:

      1. MinKey (internal type)
      2. Null
      3. Numbers (ints, longs, doubles, decimals)
      4. Symbol, String
      5. Object
      6. Array
      7. BinData
      8. ObjectId
      9. Boolean
      10. Date
      11. Timestamp
      12. Regular Expression
      13. MaxKey (internal type)

      However at least for object and array types the sort order is different:

      If create 2 objects:
       

      db.test.insertOne({"key" : { "key" : "value" } })
      db.test.insertOne({"key" : [ "value", "value" ]})

       

      And perform sorting via cursor or aggregate the sort order will be different:

      db.test.find().sort({"key":1})
      

      { "_id" : ObjectId("5b7c585ee9aa2bf28b64c184"), "key" : [ "value", "value" ] }
      { "_id" : ObjectId("5b7c553c431906bbc5028804"), "key" : { "key" : "value" } }
      

      db.test.aggregate([{"$sort":{"key":1}}])
      

      { "_id" : ObjectId("5b7c585ee9aa2bf28b64c184"), "key" : [ "value", "value" ] }
      { "_id" : ObjectId("5b7c553c431906bbc5028804"), "key" : { "key" : "value" } }
      

      NB: in Mongo 3.4.10 the output will be different: cursor sort will return object first.
      ----------------------------

      Description

      Scope of changes (files that need work and how much)

      Impact to other docs outside of this product

      MVP (work and date?)

      Resources (e.g. Scope Docs, Invision)

      Attachments

        Activity

          People

            Unassigned Unassigned
            irlyanov Dmitry Irlyanov
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              1 year, 14 weeks, 2 days ago