Skip to content

Commit fc9b67a

Browse files
authored
DOCS 14287 - multi update failure documentation (#1531) (#1579)
* 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 c9f4182 commit fc9b67a

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

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+
200205
* - ``collation``
201206

202207
- document
@@ -335,6 +340,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
335340
be ``true``. The :dbcommand:`update` command *does not* replace the
336341
``_id`` value.
337342

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

340412
Update with an Aggregation Pipeline

0 commit comments

Comments
 (0)