Skip to content

Commit a6fca7f

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

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

191+
When updating multiple documents, if a single document fails
192+
to update, further documents are not updated. See
193+
:ref:`multi-update failures <multi-update-failures>` for more
194+
details on this behavior.
195+
191196
* - ``collation``
192197

193198
- document
@@ -324,6 +329,73 @@ replace a *single* matching document; i.e. the ``multi`` field cannot
324329
be ``true``. The :dbcommand:`update` command *does not* replace the
325330
``_id`` value.
326331

332+
.. _multi-update-failures:
333+
334+
Multi-Update Failures
335+
~~~~~~~~~~~~~~~~~~~~~
336+
337+
If a single document fails to update in an update command with the
338+
``multi`` parameter set to ``true``, no further documents
339+
update as part of that command.
340+
341+
For example, create a ``members`` collection with the following documents:
342+
343+
.. code-block:: javascript
344+
345+
db.members.insertMany( [
346+
{ "_id" : 1, "member" : "Taylor", "status" : "pending", "points" : 1},
347+
{ "_id" : 2, "member" : "Alexis", "status" : "enrolled", "points" : 59},
348+
{ "_id" : 3, "member" : "Elizabeth", "status" : "enrolled", "points" : 34}
349+
] )
350+
351+
The following operation creates a document validator on the
352+
``members`` collection with a rule that the ``points`` value
353+
can not equal ``60``.
354+
355+
.. code-block:: javascript
356+
357+
db.runCommand( {
358+
collMod: "members",
359+
validator: { points: { $ne: 60 } }
360+
} )
361+
362+
This update command increases the ``points`` field of every document
363+
by ``1``.
364+
365+
.. code-block:: javascript
366+
367+
db.runCommand(
368+
{
369+
update: "members",
370+
updates: [
371+
{
372+
q: {},
373+
u: { $inc: { points: 1 } },
374+
multi: true
375+
}
376+
]
377+
}
378+
)
379+
380+
After running the command, the collection contains the following
381+
documents:
382+
383+
.. code-block:: javascript
384+
:copyable: false
385+
386+
{ _id: 1, member: 'Taylor', status: 'A', points: 2 }
387+
{ _id: 2, member: 'Alexis', status: 'D', points: 59 }
388+
{ _id: 3, member: 'Elizabeth', status: 'C', points: 34 }
389+
390+
The update command updated the ``points`` value of the first document
391+
but failed to update the second document because of the validator rule
392+
that the ``points`` value can not equal ``60``. The third document did
393+
not update because no further documents update following a write error.
394+
395+
.. seealso::
396+
397+
:ref:`Schema Validation<schema-validation-overview>`
398+
327399
.. _update-command-behaviors-aggregation-pipeline:
328400

329401
Update with an Aggregation Pipeline

0 commit comments

Comments
 (0)