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