@@ -188,6 +188,11 @@ Each document contains the following fields:
188
188
``false``, limit the update to one document that meet the query
189
189
criteria. Defaults to ``false``.
190
190
191
+ When updating multiple documents, if a single document fails
192
+ to update, further documents are not updated. See
193
+ :ref:`multi-update failures <multi-update-failures>` for more
194
+ details on this behavior.
195
+
191
196
* - ``collation``
192
197
193
198
- document
@@ -324,6 +329,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
324
329
be ``true``. The :dbcommand:`update` command *does not* replace the
325
330
``_id`` value.
326
331
332
+ .. _multi-update-failures:
333
+
334
+ Multi-Update Failures
335
+ ~~~~~~~~~~~~~~~~~~~~~
336
+
337
+ If a single document fails to update in an update command with the
338
+ ``multi`` parameter set to ``true``, no further documents
339
+ update as part of that command.
340
+
341
+ For example, create a ``members`` collection with the following documents:
342
+
343
+ .. code-block:: javascript
344
+
345
+ db.members.insertMany( [
346
+ { "_id" : 1, "member" : "Taylor", "status" : "pending", "points" : 1},
347
+ { "_id" : 2, "member" : "Alexis", "status" : "enrolled", "points" : 59},
348
+ { "_id" : 3, "member" : "Elizabeth", "status" : "enrolled", "points" : 34}
349
+ ] )
350
+
351
+ The following operation creates a document validator on the
352
+ ``members`` collection with a rule that the ``points`` value
353
+ can not equal ``60``.
354
+
355
+ .. code-block:: javascript
356
+
357
+ db.runCommand( {
358
+ collMod: "members",
359
+ validator: { points: { $ne: 60 } }
360
+ } )
361
+
362
+ This update command increases the ``points`` field of every document
363
+ by ``1``.
364
+
365
+ .. code-block:: javascript
366
+
367
+ db.runCommand(
368
+ {
369
+ update: "members",
370
+ updates: [
371
+ {
372
+ q: {},
373
+ u: { $inc: { points: 1 } },
374
+ multi: true
375
+ }
376
+ ]
377
+ }
378
+ )
379
+
380
+ After running the command, the collection contains the following
381
+ documents:
382
+
383
+ .. code-block:: javascript
384
+ :copyable: false
385
+
386
+ { _id: 1, member: 'Taylor', status: 'A', points: 2 }
387
+ { _id: 2, member: 'Alexis', status: 'D', points: 59 }
388
+ { _id: 3, member: 'Elizabeth', status: 'C', points: 34 }
389
+
390
+ The update command updated the ``points`` value of the first document
391
+ but failed to update the second document because of the validator rule
392
+ that the ``points`` value can not equal ``60``. The third document did
393
+ not update because no further documents update following a write error.
394
+
395
+ .. seealso::
396
+
397
+ :ref:`Schema Validation<schema-validation-overview>`
398
+
327
399
.. _update-command-behaviors-aggregation-pipeline:
328
400
329
401
Update with an Aggregation Pipeline
0 commit comments