@@ -9,21 +9,20 @@ $rename
99 *Syntax*: ``{$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }``
1010
1111 The :operator:`$rename` operator updates the name of a field. The
12- new field name must differ from the existing field name.
12+ new field name must differ from the old field name.
1313
1414 Consider the following example:
1515
1616 .. code-block:: javascript
1717
1818 db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )
1919
20- This operation The query renames the field ``nickname`` to ``alias``, and the
20+ This operation renames the field ``nickname`` to ``alias``, and the
2121 field ``cell`` to ``mobile``.
2222
23- If the document already has a field with *new* field name,
24- :operator:`$rename` renames the field with the *old* field name to
25- the new field name, and removes the previous field with the "*new*"
26- field name.
23+ If the document already has a field with the *new* field name, the
24+ :operator:`$rename` operator removes that field and renames the
25+ field with the *old* field name to the *new* field name.
2726
2827 The :operator:`$rename` operator will expand arrays and
2928 sub-documents to find a match for field names. When renaming a field
@@ -48,7 +47,7 @@ $rename
4847
4948 db.students.update( { _id: 1 }, { $rename: { "nmae": "name" } } )
5049
51- The query renames the sub-document ``nmae`` to ``name``:
50+ The operation renames the sub-document ``nmae`` to ``name``:
5251
5352 .. code-block:: javascript
5453
@@ -68,7 +67,7 @@ $rename
6867
6968 db.students.update( { _id: 1 }, { $rename: { "name.first": "name.fname" } } )
7069
71- The query renames the sub-document field ``first`` to ``fname``:
70+ The operation renames the sub-document field ``first`` to ``fname``:
7271
7372 .. code-block:: javascript
7473
@@ -88,7 +87,7 @@ $rename
8887
8988 db.students.update( { _id: 1 }, { $rename: { "name.last": "contact.lname" } } )
9089
91- The query renames the sub-document field ``last`` to ``lname`` and
90+ The operation renames the sub-document field ``last`` to ``lname`` and
9291 moves it to the sub-document ``contact``:
9392
9493 .. code-block:: javascript
@@ -104,7 +103,8 @@ $rename
104103 field moves out of the subdocument and becomes a regular document
105104 field.
106105
107- Consider the following behavior when the specified old field name does not exist:
106+ Consider the following behavior when the specified old field name
107+ does not exist:
108108
109109 - When renaming a single field and the old field name refers to a
110110 non-existing field, the :operator:`$rename` operator does
@@ -114,69 +114,85 @@ $rename
114114
115115 db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )
116116
117- The above query performs no action since the field with the name
117+ The operation performs no action since the field with the name
118118 ``wife`` does not exist.
119119
120- - When renaming multiple fields and **all** of the old field names refer to
121- non-existing fields, the :operator:`$rename` operator does
122- nothing, as in the following:
120+ - When renaming multiple fields and **all** of the old field names
121+ refer to non-existing fields, the :operator:`$rename` operator
122+ does nothing, as in the following:
123123
124124 .. code-block:: javascript
125125
126- db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse', 'vice': 'vp', 'office': 'term' } } )
126+ db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse',
127+ 'vice': 'vp',
128+ 'office': 'term' } } )
127129
128- The above query performs no action since fields with the name
130+ The operation performs no action since fields with the names
129131 ``wife``, ``vice``, and ``office`` do not exist.
130132
131- - When renaming multiple fields and **some** but not all `` old names``
132- refer to non-existing fields, the :operator:`$rename` operator
133- performs the following operations:
133+ - When renaming multiple fields and **some** but not all old field
134+ names refer to non-existing fields, the :operator:`$rename`
135+ operator performs the following operations:
134136
135137 .. versionchanged:: 2.2
136138
137139 - Renames the fields that exist to the specified new field names.
138140
139- - In version 2.2, :operator:`$rename` ignores the non-existing fields.
141+ - Ignores the non-existing fields.
140142
141- - Before 2.2:
142-
143- - **if** no field exists with the new field name,
144- :operator:`$rename` does nothing to any field.
145-
146- - **if** fields already exist with the new field names,
147- :operator:`$rename` drops these fields.
148-
149143 Consider the following query that renames both an existing field
150144 ``mobile`` and a non-existing field ``wife``. The field named
151145 ``wife`` does not exist and :operator:`$rename` sets the field to
152146 a name that already exists ``alias``.
153147
154148 .. code-block:: javascript
155149
156- db.students.update( { _id: 1 }, { $rename: { 'wife': 'alias', 'mobile': 'cell' } } )
150+ db.students.update( { _id: 1 }, { $rename: { 'wife': 'alias',
151+ 'mobile': 'cell' } } )
157152
158- In version 2.2, the operation renames the ``mobile`` field to
159- ``cell``. No other action occurs.
153+ The operation renames the ``mobile`` field to ``cell``. No other
154+ action occurs.
160155
161156 .. code-block:: javascript
162157
163- { "_id" : 1,
164- "alias" : [ "The American Cincinnatus", "The American Fabius" ],
165- "cell" : "555-555-5555",
166- "name" : { "lname" : "washington" },
167- "places" : { "d" : "Mt Vernon", "b" : "Colonial Beach" }
158+ { "_id" : 1,
159+ "alias" : [ "The American Cincinnatus", "The American Fabius" ],
160+ "cell" : "555-555-5555",
161+ "name" : { "lname" : "washington" },
162+ "places" : { "d" : "Mt Vernon", "b" : "Colonial Beach" }
168163 }
169164
170- Before 2.2, the operation would rename the field ``mobile`` to
171- ``cell``. The operation drops the ``alias`` field even though the
172- field ``wife`` does not exist:
173165
174- .. code-block :: javascript
166+ .. note ::
175167
176- { "_id" : 1,
177- "cell" : "555-555-5555",
178- "name" : { "lname" : "washington" },
179- "places" : { "d" : "Mt Vernon", "b" : "Colonial Beach" }
180- }
168+ Before version 2.2, when renaming multiple fields and some but
169+ not all old field names refer to non-existing fields:
170+
171+ - **if** no field exists with the new field name, the
172+ :operator:`$rename` operator does nothing.
173+
174+ - **if** fields already exist with the new field names, the
175+ :operator:`$rename` operator drops these fields.
176+
177+ Consider the following query that renames both an existing
178+ field ``mobile`` and a non-existing field ``wife``. The
179+ non-existing field ``wife`` is to be set to ``alias``, a name
180+ that already exists.
181+
182+ .. code-block:: javascript
183+
184+ db.students.update( { _id: 1 }, { $rename: { 'wife': 'alias', 'mobile': 'cell' } } )
185+
186+ Before 2.2, the operation renames the field ``mobile`` to
187+ ``cell`` *and* drops the ``alias`` field even though the field
188+ ``wife`` does not exist:
189+
190+ .. code-block:: javascript
191+
192+ { "_id" : 1,
193+ "cell" : "555-555-5555",
194+ "name" : { "lname" : "washington" },
195+ "places" : { "d" : "Mt Vernon", "b" : "Colonial Beach" }
196+ }
181197
182198 .. versionadded:: 1.7.2
0 commit comments