Skip to content

Commit c7f400b

Browse files
DOCSP-22789 add indexes behavior for rename (#6216) (#6375)
* add indexes behavior for rename * wording * wording * remove 'you must' * review feedback * more edits * wording * wording * updates * edits * formatting * formatting * review feedback
1 parent f1ac059 commit c7f400b

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

source/reference/operator/update/rename.txt

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -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

4042
Behavior
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

6592
Examples
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-

source/tutorial/manage-indexes.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Remove Indexes
4141

4242
.. include:: /includes/driver-remove-indexes-tabs.rst
4343

44+
.. _manage-indexes-modify:
45+
4446
Modify an Index
4547
---------------
4648

0 commit comments

Comments
 (0)