@@ -32,15 +32,44 @@ Concurrency Control
32
32
Concurrency control allows multiple applications to run concurrently
33
33
without causing data inconsistency or conflicts.
34
34
35
- One approach is to create a :ref:`unique index <index-type-unique>` on a
36
- field that can only have unique values. This prevents insertions or
37
- updates from creating duplicate data. Create a unique index on multiple
38
- fields to force uniqueness on that combination of field values. For
39
- examples of use cases, see :ref:`update() and Unique Index
40
- <update-with-unique-indexes>` and :ref:`findAndModify() and Unique Index
41
- <upsert-and-unique-index>`.
35
+ A :dbcommand:`findAndModify` operation on a document is atomic: if the
36
+ find condition matches a document, the update is performed on that
37
+ document. Concurrent queries and additional updates on that document are
38
+ not affected until the current update is complete.
39
+
40
+ Consider the following example:
41
+
42
+ - A collection with two documents:
43
+
44
+ .. code-block:: javascript
45
+
46
+ db.myCollection.insertMany( [
47
+ { _id: 0, a: 1, b: 1 },
48
+ { _id: 1, a: 1, b: 1 }
49
+ ] )
50
+
51
+ - Two of the following :dbcommand:`findAndModify` operations run
52
+ concurrently:
42
53
43
- Another approach is to specify the expected current value of a field in
44
- the query predicate for the write operations.
54
+ .. code-block:: javascript
55
+
56
+ db.myCollection.findAndModify( {
57
+ query: { a: 1 },
58
+ update: { $inc: { b: 1 }, $set: { a: 2 } }
59
+ } )
60
+
61
+ After the :dbcommand:`findAndModify` operations are complete, it is
62
+ guaranteed that ``a`` and ``b`` in both documents are set to ``2``.
63
+
64
+ .. seealso::
65
+
66
+ :ref:`findAndModify() Upsert Example <findAndModify-upsert-example>`
67
+
68
+ You can also create a :ref:`unique index <index-type-unique>` on a field
69
+ so that it can only have unique values. This prevents inserts and
70
+ updates from creating duplicate data. You can create a unique index
71
+ on multiple fields to ensure the combination of field values is unique.
72
+ For an example, see :ref:`findAndModify() Upsert with Unique Index
73
+ <upsert-and-unique-index>`.
45
74
46
75
.. seealso:: :doc:`/core/read-isolation-consistency-recency`
0 commit comments