Skip to content

Commit 9200afe

Browse files
jmd-mongojeff-allen-mongo
authored andcommitted
DOCS-14472 update operators act in lexicographic order
1 parent 1de1828 commit 9200afe

24 files changed

+84
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In MongoDB 4.4 and earlier, update operators process document fields
2+
in lexicographic order. See :ref:`Update Operators Behavior
3+
<update-operators-lexicographic-order>` for details.

source/reference/operator/update.txt

+24
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,30 @@ Specify the operator expression in a document of the form:
3333
Update Operators
3434
----------------
3535

36+
.. _update-operators-lexicographic-order:
37+
38+
Behavior
39+
~~~~~~~~
40+
41+
In MongoDB 4.4 and earlier, update operators process all document fields
42+
in lexicographic order.
43+
44+
In MongoDB 5.0 and later, update operators process document fields with
45+
string-based names in lexicographic order. Fields with numeric names are
46+
processed in numeric order.
47+
48+
Consider this example :update:`$set` command:
49+
50+
.. code-block:: javascript
51+
52+
{ $set: { "a.2": <new value>, "a.10": <new value>, } }
53+
54+
In MongoDB 4.4, ``"a.10"`` is processed before ``"a.2"`` because ``10``
55+
comes before ``2`` in lexicographic order.
56+
57+
In MongoDB 5.0, ``"a.2"`` is processed before ``"a.10"`` because ``2``
58+
comes before ``10`` in numeric order.
59+
3660
Fields
3761
~~~~~~
3862

source/reference/operator/update/addToSet.txt

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Definition
3030
Behavior
3131
--------
3232

33+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
34+
3335
:update:`$addToSet` only ensures that there are no duplicate items
3436
*added* to the set and does not affect existing duplicate elements.
3537
:update:`$addToSet` does not guarantee a particular ordering of

source/reference/operator/update/bit.txt

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ Definition
3737
constructor to specify integers. See :ref:`shell-type-int` or
3838
:ref:`shell-type-long` for more information.
3939

40+
Behavior
41+
--------
42+
43+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
44+
4045
Examples
4146
--------
4247

source/reference/operator/update/currentDate.txt

+2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ Definition
4242
Behavior
4343
--------
4444

45+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
46+
4547
If the field does not exist, :update:`$currentDate` adds the field to a
4648
document.
4749

source/reference/operator/update/each.txt

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ Definition
3737
other modifiers. For a list of modifiers available for
3838
:update:`$push`, see :ref:`push-modifiers`.
3939

40+
Behavior
41+
--------
42+
43+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
44+
4045
Examples
4146
--------
4247

source/reference/operator/update/inc.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ Definition
2323
{ $inc: { <field1>: <amount1>, <field2>: <amount2>, ... } }
2424

2525
.. include:: /includes/use-dot-notation.rst
26-
26+
2727
Behavior
2828
--------
2929

30+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
31+
3032
The :update:`$inc` operator accepts positive and negative values.
3133

3234
If the field does not exist, :update:`$inc` creates the field and sets

source/reference/operator/update/max.txt

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Definition
3232
Behavior
3333
--------
3434

35+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
36+
3537
If the field does not exists, the :update:`$max` operator sets the
3638
field to the specified value.
3739

source/reference/operator/update/min.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ Definition
3030
Behavior
3131
--------
3232

33-
If the field does not exists, the :update:`$min` operator sets the
33+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
34+
35+
If the field does not exist, the :update:`$min` operator sets the
3436
field to the specified value.
3537

3638
For comparisons between values of different types, such as a number and

source/reference/operator/update/mul.txt

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Definition
2929
Behavior
3030
--------
3131

32+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
33+
3234
Missing Field
3335
~~~~~~~~~~~~~
3436

source/reference/operator/update/pop.txt

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Definition
3131
Behavior
3232
--------
3333

34+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
35+
3436
The :update:`$pop` operation fails if the ``<field>`` is not an array.
3537

3638
If the :update:`$pop` operator removes the last item in the

source/reference/operator/update/position.txt

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ Definition
5858
greater than or equal to the length of the array, the :update:`$push`
5959
adds elements to the beginning of the array.
6060

61+
Behavior
62+
--------
63+
64+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
6165

6266
Examples
6367
--------

source/reference/operator/update/positional-all.txt

+2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ Definition
4343
Behavior
4444
--------
4545

46+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
47+
4648
``upsert``
4749
~~~~~~~~~~~
4850

source/reference/operator/update/positional-filtered.txt

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ Definition
5252
Behavior
5353
--------
5454

55+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
56+
5557
``upsert``
5658
~~~~~~~~~~~
5759

source/reference/operator/update/positional.txt

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ Definition
5959
Behavior
6060
--------
6161

62+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
63+
6264
``upsert``
6365
~~~~~~~~~~~
6466

source/reference/operator/update/pull.txt

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ $pull
2626
Behavior
2727
--------
2828

29+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
30+
2931
If you specify a ``<condition>`` and the array elements are embedded
3032
documents, :update:`$pull` operator applies the ``<condition>`` as if each
3133
array element were a document in a collection. See

source/reference/operator/update/pullAll.txt

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Definition
3131
Behavior
3232
--------
3333

34+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
35+
3436
If a ``<value>`` to remove is a document or an array,
3537
:update:`$pullAll` removes only the elements in the array that match
3638
the specified ``<value>`` exactly, including order.

source/reference/operator/update/push.txt

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Definition
2828
Behavior
2929
---------
3030

31+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
32+
3133
If the field is absent in the document to update, :update:`$push` adds
3234
the array field with the value as its element.
3335

source/reference/operator/update/rename.txt

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Definition
3737
Behavior
3838
--------
3939

40+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
41+
4042
The :update:`$rename` operator logically performs an :update:`$unset`
4143
of both the old name and the new name, and then performs a
4244
:update:`$set` operation with the new name. As such, the operation may

source/reference/operator/update/set.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ Definition
3232

3333
.. include:: /includes/use-dot-notation.rst
3434

35-
36-
3735
Behavior
3836
--------
3937

38+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
39+
4040
If the field does not exist, :update:`$set` will add a new field with the
4141
specified value, provided that the new field does not violate a type
4242
constraint.

source/reference/operator/update/setOnInsert.txt

+5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ Definition
3535

3636
.. include:: /includes/use-dot-notation.rst
3737

38+
Behavior
39+
--------
40+
41+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
42+
3843
Example
3944
--------
4045

source/reference/operator/update/slice.txt

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ $slice
5858
Behavior
5959
--------
6060

61+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
62+
6163
The order in which the modifiers appear is immaterial. Previous
6264
versions required the :update:`$each` modifier to appear as the first
6365
modifier if used in conjunction with :update:`$slice`. For a list of

source/reference/operator/update/sort.txt

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ $sort
4747
Behavior
4848
--------
4949

50+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
51+
5052
The :update:`$sort` modifier can sort array elements that are not
5153
documents. In previous versions, the :update:`$sort` modifier required
5254
the array elements be documents.

source/reference/operator/update/unset.txt

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ Definition
3636
Behavior
3737
--------
3838

39+
.. include:: /includes/fact-update-operator-lexicographic-order.rst
40+
3941
If the field does not exist, then :update:`$unset` does nothing (i.e.
4042
no operation).
4143

0 commit comments

Comments
 (0)