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

How should 'numericOrdering: true' handle decimals and negatives?

    • Type: Icon: Question Question
    • Resolution: Won't Do
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Execution

      'numericOrdering: true' lets you sort strings numerically rather than lexicographically. A typical use would be something like:

      > db.chapters.find({}, {_id: 0}).sort({title: 1}).collation({locale: 'en_US', numericOrdering: true})
      { "title" : "Book 1 Chapter 1" }
      { "title" : "Book 1 Chapter 2" }
      ...
      { "title" : "Book 1 Chapter 9" }
      { "title" : "Book 1 Chapter 10" }
      { "title" : "Book 1 Chapter 11" }
      { "title" : "Book 2 Chapter 1" }
      ...
      { "title" : "Book 9 Chapter 1" }
      ...
      { "title" : "Book 10 Chapter 1" }
      

      Lexicographically, "10" < "9", but with 'numericOrdering: true' it treats any numeric portion of the string as a number. There can be several numbers in one string.

      This leads to some confusing behavior if you want to sort decimals or negative numbers:

      > db.c.find({}, {_id: 0}).sort({n: 1}).collation({locale: 'en_US', numericOrdering: true})
      { "n" : "-1" }
      { "n" : "-2" }
      { "n" : "2.1" }
      { "n" : "2.2" }
      { "n" : "2.10" }
      { "n" : "2.20" }
      

      The dot and minus sign are not interpreted as part of the number. So:

      • "-1" < "-2" because "1" < "2"
      • "2.2" < "2.10" because "2" < "10"

      This behavior is useful for sorting version strings, where the dot is really a separator and not a decimal point. You would want "4.4.9" < "4.4.10" and "4.9" < "4.10".

      We should maybe document this more clearly. We could also consider adding another kind of collation, that interprets more kinds of strings numerically.

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            david.percy@mongodb.com David Percy
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: