@@ -203,6 +203,11 @@ Each document contains the following fields:
203
203
``false``, limit the update to one document that meet the query
204
204
criteria. Defaults to ``false``.
205
205
206
+ When updating multiple documents, if a single document fails
207
+ to update, further documents are not updated. See
208
+ :ref:`multi-update failures <multi-update-failures>` for more
209
+ details on this behavior.
210
+
206
211
* - ``collation``
207
212
208
213
- document
@@ -341,6 +346,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
341
346
be ``true``. The :dbcommand:`update` command *does not* replace the
342
347
``_id`` value.
343
348
349
+ .. _multi-update-failures:
350
+
351
+ Multi-Update Failures
352
+ ~~~~~~~~~~~~~~~~~~~~~
353
+
354
+ If a single document fails to update in an update command with the
355
+ ``multi`` parameter set to ``true``, no further documents
356
+ update as part of that command.
357
+
358
+ For example, create a ``members`` collection with the following documents:
359
+
360
+ .. code-block:: javascript
361
+
362
+ db.members.insertMany( [
363
+ { "_id" : 1, "member" : "Taylor", "status" : "pending", "points" : 1},
364
+ { "_id" : 2, "member" : "Alexis", "status" : "enrolled", "points" : 59},
365
+ { "_id" : 3, "member" : "Elizabeth", "status" : "enrolled", "points" : 34}
366
+ ] )
367
+
368
+ The following operation creates a document validator on the
369
+ ``members`` collection with a rule that the ``points`` value
370
+ can not equal ``60``.
371
+
372
+ .. code-block:: javascript
373
+
374
+ db.runCommand( {
375
+ collMod: "members",
376
+ validator: { points: { $ne: 60 } }
377
+ } )
378
+
379
+ This update command increases the ``points`` field of every document
380
+ by ``1``.
381
+
382
+ .. code-block:: javascript
383
+
384
+ db.runCommand(
385
+ {
386
+ update: "members",
387
+ updates: [
388
+ {
389
+ q: {},
390
+ u: { $inc: { points: 1 } },
391
+ multi: true
392
+ }
393
+ ]
394
+ }
395
+ )
396
+
397
+ After running the command, the collection contains the following
398
+ documents:
399
+
400
+ .. code-block:: javascript
401
+ :copyable: false
402
+
403
+ { _id: 1, member: 'Taylor', status: 'A', points: 2 }
404
+ { _id: 2, member: 'Alexis', status: 'D', points: 59 }
405
+ { _id: 3, member: 'Elizabeth', status: 'C', points: 34 }
406
+
407
+ The update command updated the ``points`` value of the first document
408
+ but failed to update the second document because of the validator rule
409
+ that the ``points`` value can not equal ``60``. The third document did
410
+ not update because no further documents update following a write error.
411
+
412
+ .. seealso::
413
+
414
+ :ref:`Schema Validation<schema-validation-overview>`
415
+
344
416
.. _update-command-behaviors-aggregation-pipeline:
345
417
346
418
Update with an Aggregation Pipeline
0 commit comments