Skip to content

Commit f6663d3

Browse files
jason-price-mongodbjason-price-mongodb
andauthored
DOCSP-11906 find and modify atomicity (#661) (#704)
* DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity * DOCSP-11906-find-and-modify-atomicity Co-authored-by: jason-price-mongodb <[email protected]> Co-authored-by: jason-price-mongodb <[email protected]>
1 parent 656f534 commit f6663d3

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

source/core/write-operations-atomicity.txt

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,46 @@ Concurrency Control
3434
Concurrency control allows multiple applications to run concurrently
3535
without causing data inconsistency or conflicts.
3636

37-
One approach is to create a :ref:`unique index <index-type-unique>` on a
38-
field that can only have unique values. This prevents insertions or
39-
updates from creating duplicate data. Create a unique index on multiple
40-
fields to force uniqueness on that combination of field values. For
41-
examples of use cases, see :ref:`findAndModify() and Unique Index
42-
<upsert-and-unique-index>`.
37+
A :dbcommand:`findAndModify` operation on a document is :term:`atomic
38+
<atomic operation>`: if the find condition matches a document, the
39+
update is performed on that document. Concurrent queries and additional
40+
updates on that document are not affected until the current update is
41+
complete.
42+
43+
Consider the following example:
44+
45+
- A collection with two documents:
46+
47+
.. code-block:: javascript
48+
49+
db.myCollection.insertMany( [
50+
{ _id: 0, a: 1, b: 1 },
51+
{ _id: 1, a: 1, b: 1 }
52+
] )
53+
54+
- Two of the following :dbcommand:`findAndModify` operations run
55+
concurrently:
56+
57+
.. code-block:: javascript
4358

44-
Another approach is to specify the expected current value of a field in
45-
the query predicate for the write operations.
59+
db.myCollection.findAndModify( {
60+
query: { a: 1 },
61+
update: { $inc: { b: 1 }, $set: { a: 2 } }
62+
} )
63+
64+
After the :dbcommand:`findAndModify` operations are complete, it is
65+
guaranteed that ``a`` and ``b`` in both documents are set to ``2``.
66+
67+
.. seealso::
68+
69+
:ref:`findAndModify() Upsert Example <findAndModify-upsert-example>`
70+
71+
You can also create a :ref:`unique index <index-type-unique>` on a field
72+
so that it can only have unique values. This prevents inserts and
73+
updates from creating duplicate data. You can create a unique index
74+
on multiple fields to ensure the combination of field values is unique.
75+
For an example, see :ref:`findAndModify() Upsert with Unique Index
76+
<upsert-and-unique-index>`.
4677

4778
.. seealso::
4879

source/reference/method/db.collection.findAndModify.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ This method performs the following actions:
451451
If no document matched the ``query`` condition, the method
452452
returns ``null``.
453453

454+
.. _findAndModify-upsert-example:
455+
454456
Upsert
455457
~~~~~~
456458

0 commit comments

Comments
 (0)