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