Skip to content

Commit c65d40d

Browse files
DOCSP-19919 Mention limitations to collation document's 'numericOrdering' option (#289)
* DOCSP-19919 init * code-block none * * * Added code example in restrictions section * remove the * code block indent fix * remove comma * Apply suggestions from code review Co-authored-by: jeff-allen-mongo <[email protected]> * * * Add addition bullet for clarity * Address Kyle comments #1 * add 3 docs back to insert * numericOrder -> numericOrdering Co-authored-by: jeff-allen-mongo <[email protected]>
1 parent c661da4 commit c65d40d

File tree

2 files changed

+104
-10
lines changed

2 files changed

+104
-10
lines changed

source/includes/extracts-collation.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ content: |
4343
4444
The collation option has the following syntax:
4545
46-
.. code-block:: javascript
46+
.. code-block:: none
4747
4848
collation: {
4949
locale: <string>,
@@ -62,7 +62,7 @@ content: |
6262
---
6363
ref: collation-document
6464
content: |
65-
.. code-block:: javascript
65+
.. code-block:: none
6666
6767
{
6868
locale: <string>,

source/reference/collation.txt

Lines changed: 102 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ parameters and the locales they are associated with, see
119119

120120
- boolean
121121

122-
- Optional. Flag that determines whether to include case comparison at
123-
``strength`` level ``1`` or ``2``.
122+
- Optional. Flag that determines whether to include case comparison
123+
at ``strength`` level ``1`` or ``2``.
124124

125-
If ``true``, include case comparison; i.e.
125+
If ``true``, include case comparison:
126126

127127
- When used with ``strength:1``, collation compares base characters
128128
and case.
@@ -176,12 +176,15 @@ parameters and the locales they are associated with, see
176176
- Optional. Flag that determines whether to compare numeric strings as numbers
177177
or as strings.
178178

179-
If ``true``, compare as numbers; i.e. ``"10"`` is greater than
180-
``"2"``.
179+
If ``true``, compare as numbers. For example,
180+
``"10"`` is greater than ``"2"``.
181181

182-
If ``false``, compare as strings; i.e. ``"10"`` is less than ``"2"``.
182+
If ``false``, compare as strings. For example,
183+
``"10"`` is less than ``"2"``.
183184

184185
Default is ``false``.
186+
187+
See :ref:`numericOrdering Restrictions <numeric-order-restrictions>`.
185188

186189

187190

@@ -237,12 +240,12 @@ parameters and the locales they are associated with, see
237240

238241
* - ``"punct"``
239242

240-
- Both whitespace and punctuation are "ignorable", i.e. not
243+
- Both whitespace and punctuation are ignorable and not
241244
considered base characters.
242245

243246
* - ``"space"``
244247

245-
- Whitespace are "ignorable", i.e. not considered base
248+
- Whitespace is ignorable and not considered to be base
246249
characters.
247250

248251

@@ -334,7 +337,98 @@ Collation and Unsupported Index Types
334337
.. include:: /includes/extracts/collation-index-type-restrictions.rst
335338

336339
.. include:: /includes/extracts/collation-index-type-restrictions-addendum.rst
340+
341+
Restrictions
342+
------------
343+
344+
.. _numeric-order-restrictions:
345+
346+
numericOrdering
347+
~~~~~~~~~~~~~~~
348+
349+
When specifying the ``numericOrdering`` as ``true`` the following
350+
restrictions apply:
351+
352+
- Only contiguous non-negative integer substrings of digits are
353+
considered in the comparisons.
354+
355+
``numericOrdering`` does not support:
356+
357+
- ``+``
358+
- ``-``
359+
- decimal separators, like decimal points and decimal commas
360+
- exponents
361+
362+
- Only Unicode code points in the Number or Decimal Digit (Nd) category
363+
are treated as digits.
364+
365+
- If a digit length exceeds 254 characters, the excess characters are
366+
treated as a separate number.
367+
368+
Consider a collection with the following string number and decimal
369+
values:
370+
371+
.. code-block:: javascript
372+
:emphasize-lines: 6,10
373+
374+
db.c.insertMany(
375+
[
376+
{ "n" : "1" },
377+
{ "n" : "2" },
378+
{ "n" : "2.1" },
379+
{ "n" : "-2.1" },
380+
{ "n" : "2.2" },
381+
{ "n" : "2.10" },
382+
{ "n" : "2.20" },
383+
{ "n" : "-10" },
384+
{ "n" : "10" },
385+
{ "n" : "20" },
386+
{ "n" : "20.1" }
387+
]
388+
)
389+
390+
The following :method:`find <db.collection.find()>` query uses a
391+
collation document containing the ``numericOrdering`` parameter:
392+
393+
.. code-block:: javascript
394+
395+
db.c.find(
396+
{ }, { _id: 0 }
397+
).sort(
398+
{ n: 1 }
399+
).collation( {
400+
locale: 'en_US',
401+
numericOrdering: true
402+
} )
403+
404+
The operation returns the following results:
405+
406+
.. code-block:: javascript
407+
:emphasize-lines: 2-3,7-8
408+
:copyable: false
337409

410+
[
411+
{ n: '-2.1' },
412+
{ n: '-10' },
413+
{ n: '1' },
414+
{ n: '2' },
415+
{ n: '2.1' },
416+
{ n: '2.2' },
417+
{ n: '2.10' },
418+
{ n: '2.20' },
419+
{ n: '10' },
420+
{ n: '20' },
421+
{ n: '20.1' }
422+
]
423+
424+
- ``numericOrdering: true`` sorts the string values in ascending
425+
order as if they were numeric values.
426+
- The two negative values ``-2.1`` and ``-10`` are not sorted in the
427+
expected sort order because they have unsupported ``-`` characters.
428+
- The value ``2.2`` is sorted before the value ``2.10``, due to the fact
429+
that the ``numericOrdering`` parameter does not support decimal
430+
values.
431+
- As a result, ``2.2`` and ``2.10`` are sorted in lexicographic order.
338432

339433
.. toctree::
340434
:titlesonly:

0 commit comments

Comments
 (0)