-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
db.collection.updateMany.txt
289 lines (186 loc) · 7.69 KB
/
db.collection.updateMany.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
==========================
db.collection.updateMany()
==========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. method:: db.collection.updateMany(filter, update, options)
.. versionadded:: 3.2
Updates multiple documents within the collection based on the filter.
The :method:`~db.collection.updateMany()` method has the following form:
.. code-block:: javascript
db.collection.updateMany(
<filter>,
<update>,
{
upsert: <boolean>,
writeConcern: <document>
}
)
The :method:`~db.collection.updateMany()` method takes the following
parameters:
.. list-table::
:header-rows: 1
:widths: 20 20 80
* - Parameter
- Type
- Description
* - ``filter``
- document
- The selection criteria for the update. The same :ref:`query
selectors <query-selectors>` as in the :method:`find()
<db.collection.find()>` method are available.
Specify an empty document ``{ }`` to update all documents in
the collection.
* - ``update``
- document
- The modifications to apply.
Use :doc:`/reference/operator/update/` such as :update:`$set`,
:update:`$unset`, or :update:`$rename`.
Using the :ref:`update() <update-parameter>` pattern of
``field: value`` for the ``update`` parameter throws an error.
* - ``upsert``
- boolean
- .. include:: /includes/extracts/updateMany-behavior-method.rst
* - ``writeConcern``
- document
- Optional. A document expressing the :doc:`write concern
</reference/write-concern>`. Omit to use the default write concern.
:returns:
A document containing:
- A boolean ``acknowledged`` as ``true`` if the operation ran with
:term:`write concern` or ``false`` if write concern was disabled
- ``matchedCount`` containing the number of matched documents
- ``modifiedCount`` containing the number of modified documents
- ``upsertedId`` containing the ``_id`` for the upserted document
Behavior
--------
:method:`~db.collection.updateMany()` updates all matching documents in
the collection that match the ``filter``, using the ``update`` criteria
to apply modifications.
If ``upsert: true`` and no documents match the ``filter``,
:method:`~db.collection.updateMany()` creates a new
document based on the ``filter`` and ``update`` parameters. See
:ref:`updateMany-example-update-multiple-documents-with-upsert`.
.. _updateMany-capped-collections:
Capped Collections
~~~~~~~~~~~~~~~~~~
.. include:: /includes/extracts/capped-collection-immutable-document-size-update.rst
.. TBD - need to test more thoroughly to pin down func.
.. Sharded Collections
.. ~~~~~~~~~~~~~~~~~~~
Explainability
~~~~~~~~~~~~~~
.. |write-method| replace:: :method:`~db.collection.updateMany()`
.. |old-write-method| replace:: :method:`~db.collection.update()`
.. include:: /includes/fact-bulkwrite-explainable.rst
.. _updateMany-method-examples:
Examples
--------
.. _updateMany-example-update-multiple-documents:
Update Multiple Documents
~~~~~~~~~~~~~~~~~~~~~~~~~
The ``restaurant`` collection contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5 }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8 }
The following operation updates all documents where ``violations`` are
greater than ``4`` and :update:`$set` a flag for review:
.. code-block:: javascript
try {
db.restaurant.updateMany(
{ violations: { $gt: 4 } },
{ $set: { "Review" : true } }
);
} catch (e) {
print(e);
}
The operation returns:
.. code-block:: javascript
{ "acknowledged" : true, "matchedCount" : 2, "modifiedCount" : 2 }
The collection now contains the following documents:
.. code-block:: javascript
{ "_id" : 1, "name" : "Central Perk Cafe", "violations" : 3 }
{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "violations" : 2 }
{ "_id" : 3, "name" : "Empire State Sub", "violations" : 5, "Review" : true }
{ "_id" : 4, "name" : "Pizza Rat's Pizzaria", "violations" : 8, "Review" : true }
If no matches were found, the operation instead returns:
.. code-block:: javascript
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }
Setting ``upsert: true`` would insert a document if no match was found.
.. _updateMany-example-update-multiple-documents-with-upsert:
Update Multiple Documents with Upsert
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The ``inspectors`` collection contains the following documents:
.. code-block:: javascript
{ "_id" : 92412, "inspector" : "F. Drebin", "Sector" : 1, "Patrolling" : true },
{ "_id" : 92413, "inspector" : "J. Clouseau", "Sector" : 2, "Patrolling" : false },
{ "_id" : 92414, "inspector" : "J. Clouseau", "Sector" : 3, "Patrolling" : true },
{ "_id" : 92415, "inspector" : "R. Coltrane", "Sector" : 3, "Patrolling" : false }
The following operation updates all documents with ``Sector`` greater
than 4 and ``inspector`` equal to ``"R. Coltrane"``:
.. code-block:: javascript
try {
db.inspectors.updateMany(
{ "Sector" : { $gt : 4 }, "inspector" : "R. Coltrane" },
{ $set: { "Patrolling" : false } },
{ upsert: true }
);
} catch (e) {
print(e);
}
The operation returns:
.. code-block:: javascript
{
"acknowledged" : true,
"matchedCount" : 0,
"modifiedCount" : 0,
"upsertedId" : ObjectId("56fc5dcb39ee682bdc609b02")
}
The collection now contains the following documents:
.. code-block:: javascript
{ "_id" : 92412, "inspector" : "F. Drebin", "Sector" : 1, "Patrolling" : true },
{ "_id" : 92413, "inspector" : "J. Clouseau", "Sector" : 2, "Patrolling" : false },
{ "_id" : 92414, "inspector" : "J. Clouseau", "Sector" : 3, "Patrolling" : true },
{ "_id" : 92415, "inspector" : "R. Coltrane", "Sector" : 3, "Patrolling" : false },
{ "_id" : ObjectId("56fc5dcb39ee682bdc609b02"), "inspector" : "R. Coltrane", "Patrolling" : false }
Since no documents matched the filter, and ``upsert`` was ``true``,
:method:`~db.collection.updateMany` inserted the document with a
generated ``_id``, the equality conditions from the ``filter``, and the
``update`` modifiers.
.. _updateMany-example-update-with-write-concern:
Update with Write Concern
~~~~~~~~~~~~~~~~~~~~~~~~~
Given a three member replica set, the following operation specifies a
``w`` of ``majority`` and ``wtimeout`` of ``100``:
.. code-block:: javascript
try {
db.restaurant.updateMany(
{ "name" : "Pizza Rat's Pizzaria" },
{ $inc: { "violations" : 3}, $set: { "Closed" : true } },
{ w: "majority", wtimeout: 100 }
);
} catch (e) {
print(e);
}
If the acknowledgement takes longer than the ``wtimeout`` limit, the following
exception is thrown:
.. code-block:: javascript
WriteConcernError({
"code" : 64,
"errInfo" : {
"wtimeout" : true
},
"errmsg" : "waiting for replication timed out"
}) :
undefined
The ``wtimeout`` error only indicates that the operation did not complete on
time. The write operation itself can still succeed outside of the set time
limit.