@@ -34,15 +34,46 @@ Concurrency Control
34
34
Concurrency control allows multiple applications to run concurrently
35
35
without causing data inconsistency or conflicts.
36
36
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
43
58
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>`.
46
77
47
78
.. seealso::
48
79
0 commit comments