@@ -15,52 +15,79 @@ Definition
1515
1616.. update:: $rename
1717
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+ ------
1922
20- .. code-block:: javascript
23+ .. code-block:: javascript
2124
22- { $rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
25+ { $rename: { <field1>: <newName1>, <field2>: <newName2>, ... } }
2326
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`.
2730
28- Consider the following example:
31+ Consider the following example:
2932
30- .. code-block:: javascript
33+ .. code-block:: javascript
3134
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+ )
3638
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 .
3941
4042Behavior
4143--------
4244
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+
4370.. include:: /includes/fact-update-operator-processing-order.rst
4471
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 .
5077
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.
5479
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+ ~~~~~~~~~~~~~~~~~~~~
5782
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>``.
6286
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
6491
6592Examples
6693--------
@@ -102,10 +129,13 @@ name of the field and the new name:
102129
103130.. code-block:: javascript
104131
105- db.students.updateMany( {}, { $rename: { "nmae": "name" } } )
132+ db.students.updateMany(
133+ { "nmae": { $ne: null } },
134+ { $rename: { "nmae": "name" } }
135+ )
106136
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`` :
109139
110140.. code-block:: javascript
111141
@@ -123,10 +153,12 @@ in the collection:
123153 "name" : { "first" : "abigail", "last" : "adams" }
124154 }
125155
126- { "_id" : 3,
156+ {
157+ "_id" : 3,
127158 "alias" : [ "Amazing grace" ],
128159 "mobile" : "111-111-1111",
129- "name" : { "first" : "grace", "last" : "hopper" } }
160+ "name" : { "first" : "grace", "last" : "hopper" }
161+ }
130162
131163.. _rename-field-in-embedded-document:
132164
@@ -170,4 +202,3 @@ This operation does nothing because there is no field named ``wife``.
170202
171203 - :method:`db.collection.updateMany()`
172204 - :method:`db.collection.findAndModify()`
173-
0 commit comments