-
Type:
Question
-
Resolution: Won't Do
-
Priority:
Major - P3
-
None
-
Affects Version/s: None
-
Component/s: None
-
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.