@@ -9,21 +9,20 @@ $rename
9
9
*Syntax*: ``{$rename: { <old name1>: <new name1>, <old name2>: <new name2>, ... } }``
10
10
11
11
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.
13
13
14
14
Consider the following example:
15
15
16
16
.. code-block:: javascript
17
17
18
18
db.students.update( { _id: 1 }, { $rename: { 'nickname': 'alias', 'cell': 'mobile' } } )
19
19
20
- This operation The query renames the field ``nickname`` to ``alias``, and the
20
+ This operation renames the field ``nickname`` to ``alias``, and the
21
21
field ``cell`` to ``mobile``.
22
22
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.
27
26
28
27
The :operator:`$rename` operator will expand arrays and
29
28
sub-documents to find a match for field names. When renaming a field
@@ -48,7 +47,7 @@ $rename
48
47
49
48
db.students.update( { _id: 1 }, { $rename: { "nmae": "name" } } )
50
49
51
- The query renames the sub-document ``nmae`` to ``name``:
50
+ The operation renames the sub-document ``nmae`` to ``name``:
52
51
53
52
.. code-block:: javascript
54
53
@@ -68,7 +67,7 @@ $rename
68
67
69
68
db.students.update( { _id: 1 }, { $rename: { "name.first": "name.fname" } } )
70
69
71
- The query renames the sub-document field ``first`` to ``fname``:
70
+ The operation renames the sub-document field ``first`` to ``fname``:
72
71
73
72
.. code-block:: javascript
74
73
@@ -88,7 +87,7 @@ $rename
88
87
89
88
db.students.update( { _id: 1 }, { $rename: { "name.last": "contact.lname" } } )
90
89
91
- The query renames the sub-document field ``last`` to ``lname`` and
90
+ The operation renames the sub-document field ``last`` to ``lname`` and
92
91
moves it to the sub-document ``contact``:
93
92
94
93
.. code-block:: javascript
@@ -104,7 +103,8 @@ $rename
104
103
field moves out of the subdocument and becomes a regular document
105
104
field.
106
105
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:
108
108
109
109
- When renaming a single field and the old field name refers to a
110
110
non-existing field, the :operator:`$rename` operator does
@@ -114,69 +114,85 @@ $rename
114
114
115
115
db.students.update( { _id: 1 }, { $rename: { 'wife': 'spouse' } } )
116
116
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
118
118
``wife`` does not exist.
119
119
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:
123
123
124
124
.. code-block:: javascript
125
125
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' } } )
127
129
128
- The above query performs no action since fields with the name
130
+ The operation performs no action since fields with the names
129
131
``wife``, ``vice``, and ``office`` do not exist.
130
132
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:
134
136
135
137
.. versionchanged:: 2.2
136
138
137
139
- Renames the fields that exist to the specified new field names.
138
140
139
- - In version 2.2, :operator:`$rename` ignores the non-existing fields.
141
+ - Ignores the non-existing fields.
140
142
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
-
149
143
Consider the following query that renames both an existing field
150
144
``mobile`` and a non-existing field ``wife``. The field named
151
145
``wife`` does not exist and :operator:`$rename` sets the field to
152
146
a name that already exists ``alias``.
153
147
154
148
.. code-block:: javascript
155
149
156
- db.students.update( { _id: 1 }, { $rename: { 'wife': 'alias', 'mobile': 'cell' } } )
150
+ db.students.update( { _id: 1 }, { $rename: { 'wife': 'alias',
151
+ 'mobile': 'cell' } } )
157
152
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.
160
155
161
156
.. code-block:: javascript
162
157
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" }
168
163
}
169
164
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:
173
165
174
- .. code-block :: javascript
166
+ .. note ::
175
167
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
+ }
181
197
182
198
.. versionadded:: 1.7.2
0 commit comments