Skip to content

Commit 7d89b22

Browse files
authored
DOCS 14287 - multi update failure documentation (#1531) (#1577)
* 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 00fc06c commit 7d89b22

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
@@ -190,6 +190,11 @@ Each document contains the following fields:
190190
``false``, limit the update to one document that meet the query
191191
criteria. Defaults to ``false``.
192192

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+
193198
* - ``collation``
194199

195200
- document
@@ -328,6 +333,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
328333
be ``true``. The :dbcommand:`update` command *does not* replace the
329334
``_id`` value.
330335

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

333405
Update with an Aggregation Pipeline

0 commit comments

Comments
 (0)