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

Document sort order for array fields

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • v1.3.2
    • Affects Version/s: None
    • Component/s: manual
    • Labels:
      None

      If a collection contains:

      {}
      {a: 1}
      {a: []}
      {a: [1]}
      {a: [0, 1]}
      

      ... there are some bizarre semantics for how it's sorted. The empty array comes before the absence-of-a-field, and "1" comes between [0, 1] and [1]:

      > db.collection.find({}, {_id: false}).sort({a: 1})
      { "a" : [ ] }
      {  }
      { "a" : [  0,  1 ] }
      { "a" : 1 }
      { "a" : [  1 ] }
      

      Reversing the sort does not quite reverse the order of results:

      > db.collection.find({}, {_id: false}).sort({a: -1})
      { "a" : 1 }
      { "a" : [  1 ] }
      { "a" : [  0,  1 ] }
      {  }
      { "a" : [ ] }
      

      There are a number of tickets, some incomplete, regarding this confusing behavior:

            Assignee:
            kay.kim@mongodb.com Kay Kim (Inactive)
            Reporter:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              10 years, 7 weeks, 1 day ago