Skip to content

Commit 8bb001f

Browse files
authored
DOCS 14287 - multi update failure documentation (#1531) (#1578)
* DOCS-14287 adding update failure behavior * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 update failure edits * DOCS-14287 moving example and explanation * DOCS-14287 moving example and explanation * DOCS-14287 typo fix * DOCS-14287 small copy edits * DOCS-14287 minor edits * DOCS-14287 adding description under multi * DOCS-14287 adding description under multi * DOCS-14287 small tech edits * DOCS-14287 small tech edits
1 parent c414a00 commit 8bb001f

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

source/reference/command/update.txt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ Each document contains the following fields:
203203
``false``, limit the update to one document that meet the query
204204
criteria. Defaults to ``false``.
205205

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+
206211
* - ``collation``
207212

208213
- document
@@ -341,6 +346,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
341346
be ``true``. The :dbcommand:`update` command *does not* replace the
342347
``_id`` value.
343348

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+
344416
.. _update-command-behaviors-aggregation-pipeline:
345417

346418
Update with an Aggregation Pipeline

0 commit comments

Comments
 (0)