@@ -15,52 +15,79 @@ Definition
15
15
16
16
.. update:: $rename
17
17
18
- The :update:`$rename` operator updates the name of a field and has the following form:
18
+ The :update:`$rename` operator updates the name of a field.
19
+
20
+ Syntax
21
+ ------
19
22
20
- .. code-block:: javascript
23
+ .. code-block:: javascript
21
24
22
- { $rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
25
+ { $rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
23
26
24
- The new field name must differ from the existing field name. To
25
- specify a ``<field>`` in an embedded document, use :term:`dot
26
- notation`.
27
+ The new field name must differ from the existing field name. To
28
+ specify a ``<field>`` in an embedded document, use :term:`dot
29
+ notation`.
27
30
28
- Consider the following example:
31
+ Consider the following example:
29
32
30
- .. code-block:: javascript
33
+ .. code-block:: javascript
31
34
32
- db.students.updateOne(
33
- { _id: 1 },
34
- { $rename: { 'nickname': 'alias', 'cell': 'mobile' } }
35
- )
35
+ db.students.updateOne(
36
+ { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } }
37
+ )
36
38
37
- This operation renames the field ``nickname`` to ``alias``, and the
38
- field ``cell`` to ``mobile``.
39
+ The preceding operation renames the ``nickname`` field to ``alias`` and
40
+ the ``cell`` field to ``mobile`` in a document where ``_id`` is 1 .
39
41
40
42
Behavior
41
43
--------
42
44
45
+ When you run a ``$rename`` operation, MongoDB performs the following
46
+ actions:
47
+
48
+ - Delete the old ``<field>`` and field with ``<newName>`` from the
49
+ document (using :update:`$unset`).
50
+
51
+ - Perform a :update:`$set` operation with ``<newName>``, using the value
52
+ from ``<field>``.
53
+
54
+ Atomicity
55
+ ~~~~~~~~~
56
+
57
+ Each document matched by an update command is updated in an individual
58
+ operation. Update operations (like ``$rename``) only guarantee atomicity
59
+ on a single-document level.
60
+
61
+ Field Order
62
+ ~~~~~~~~~~~
63
+
64
+ The ``$rename`` operation might not preserve the order of the fields in
65
+ the document.
66
+
67
+ Update Processing Order
68
+ ~~~~~~~~~~~~~~~~~~~~~~~
69
+
43
70
.. include:: /includes/fact-update-operator-processing-order.rst
44
71
45
- The :update:`$rename` operator logically performs an :update:`$unset`
46
- of both the old name and the new name, and then performs a
47
- :update:`$set` operation with the new name. As such, the operation may
48
- not preserve the order of the fields in the document; i.e. the renamed
49
- field may move within the document .
72
+ Rename Embedded Document Fields
73
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74
+
75
+ The ``$rename`` operator can move fields into and out of embedded
76
+ documents .
50
77
51
- If the document already has a field with the ``<newName>``, the
52
- :update:`$rename` operator removes that field and renames the specified
53
- ``<field>`` to ``<newName>``.
78
+ ``$rename`` does not work on embedded documents in arrays.
54
79
55
- If the field to rename does not exist in a document, :update:`$rename`
56
- does nothing (i.e. no operation).
80
+ Other Considerations
81
+ ~~~~~~~~~~~~~~~~~~~~
57
82
58
- For fields in embedded documents, the :update:`$rename` operator can
59
- rename these fields as well as move the fields in and out of embedded
60
- documents. :update:`$rename` does not work if these fields are in array
61
- elements.
83
+ - If the document already has a field with the ``<newName>``, the
84
+ :update:`$rename` operator removes that field and renames the
85
+ specified ``<field>`` to ``<newName>``.
62
86
63
- .. include:: /includes/extracts/update-operation-empty-operand-expressions-rename.rst
87
+ - If the field to rename does not exist in a document, :update:`$rename`
88
+ does nothing.
89
+
90
+ - .. include:: /includes/extracts/update-operation-empty-operand-expressions-rename.rst
64
91
65
92
Examples
66
93
--------
@@ -102,10 +129,13 @@ name of the field and the new name:
102
129
103
130
.. code-block:: javascript
104
131
105
- db.students.updateMany( {}, { $rename: { "nmae": "name" } } )
132
+ db.students.updateMany(
133
+ { "nmae": { $ne: null } },
134
+ { $rename: { "nmae": "name" } }
135
+ )
106
136
107
- This operation renames the field ``nmae`` to ``name `` for all documents
108
- in the collection :
137
+ This operation checks for documents where the ``nmae `` field is not null
138
+ and updates those documents to rename the ``nmae`` field to ``name`` :
109
139
110
140
.. code-block:: javascript
111
141
@@ -123,10 +153,12 @@ in the collection:
123
153
"name" : { "first" : "abigail", "last" : "adams" }
124
154
}
125
155
126
- { "_id" : 3,
156
+ {
157
+ "_id" : 3,
127
158
"alias" : [ "Amazing grace" ],
128
159
"mobile" : "111-111-1111",
129
- "name" : { "first" : "grace", "last" : "hopper" } }
160
+ "name" : { "first" : "grace", "last" : "hopper" }
161
+ }
130
162
131
163
.. _rename-field-in-embedded-document:
132
164
@@ -170,4 +202,3 @@ This operation does nothing because there is no field named ``wife``.
170
202
171
203
- :method:`db.collection.updateMany()`
172
204
- :method:`db.collection.findAndModify()`
173
-
0 commit comments