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

Document sort order for array fields

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: v1.3.2
    • Component/s: manual
    • Labels:
      None
    • # Replies:
      2
    • Last comment by Customer:
      true
    • Actual Time:
      8

      Description

      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:

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              kay.kim Kay Kim
              Reporter:
              jesse A. Jesse Jiryu Davis
              Participants:
              Last commenter:
              Jonathan Dahl
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since reply:
                6 years, 11 weeks, 6 days ago
                Date of 1st Reply: