@@ -4,8 +4,8 @@ Isolate Sequence of Operations
4
4
5
5
.. default-domain:: mongodb
6
6
7
- Background
8
- ----------
7
+ Overview
8
+ --------
9
9
10
10
Write operations are atomic on the level of a single document: no
11
11
single write operation can atomically affect more than one document or
@@ -21,32 +21,38 @@ No other operations are atomic; however, you can *isolate* a
21
21
single write operation that affects multiple documents using the
22
22
:doc:`isolation operator </reference/operator/atomic>`.
23
23
24
- Additionally, the following patterns can manage a sequence of
25
- operations:
24
+ This document describes one method of updating documents *only* if the
25
+ local copy of the document reflects the current state of the document
26
+ in the database. In addition the following methods provide a way to
27
+ manage isolated sequences of operations:
26
28
27
- - :method:`findAndModify() <db.collection.findAndModify()>`
28
-
29
- - :ref:`tutorial-atomic-update-if-current`
29
+ - the :method:`findAndModify() <db.collection.findAndModify()>`
30
+ provides an isolated query and modify operation.
30
31
31
32
- :doc:`/tutorial/perform-two-phase-commits`
32
33
33
- - :method:`ensureIndex() <db.collection.ensureIndex()>` to create a
34
- ``unique`` index on a field
34
+ - Create a :ref:`unique index <index-type-unique>`, to ensure that a
35
+ key doesn't exist when you insert it.
35
36
36
37
.. _tutorial-atomic-update-if-current:
37
38
38
39
Update if Current
39
40
-----------------
40
41
41
- The "Update if Current" pattern queries a document, locally modifies
42
- various fields of the document, and tries to update the fields of a
43
- document *if* the fields have not changed in the collection since the
44
- query.
42
+ In this patter, you will:
43
+
44
+ - queries for a document,
45
+
46
+ - modifies the fields in that document
47
+
48
+ - and updates the fields of a document *only if* the fields have not
49
+ changed in the collection since the query.
45
50
46
- Consider the following example which attempts to update the ``qty``
47
- field of a document in the ``products`` collection:
51
+ Consider the following example in JavaScript which attempts to update
52
+ the ``qty`` field of a document in the ``products`` collection:
48
53
49
54
.. code-block:: javascript
55
+ :linenos:
50
56
51
57
var myCollection = db.products;
52
58
var myDocument = myCollection.findOne( { sku: 'abc123' } );
@@ -83,19 +89,23 @@ field of a document in the ``products`` collection:
83
89
84
90
}
85
91
86
- Consider the following modifications to the "Update if Current" strategy:
92
+ Your application may require some modifications of this pattern, such
93
+ as:
87
94
88
- - To generalize the strategy to guarantee that the whole document has
89
- not changed rather than just certain fields, use the entire document in
90
- the query expression.
95
+ - Use the entire document as the query in lines 18 and 19, to
96
+ generalize the operation and guarantee that the original document
97
+ was not modified, rather than ensuring that as single field was not
98
+ changed.
91
99
92
- - Add a version variable that is incremented upon each update operation
93
- to the documents. Use this version variable in the query expression.
100
+ - Add a version variable to the document that applications increment
101
+ upon each update operation to the documents. Use this version
102
+ variable in the query expression. You must be able to ensure that
103
+ *all* clients that connect to your database obey this constraint.
94
104
95
105
- Use :operator:`$set` in the update expression to modify only your
96
106
fields and prevent overriding other fields.
97
107
98
- .. Add link to :doc:`/tutorial/create-an-auto-increment-field` once that branch is merged since it's a special case
108
+ .. Add link to :doc:`/tutorial/create-an-auto-increment-field` once that branch is merged since it's a special case
99
109
100
110
.. Maybe incorporate the blurb: "MongoDB does not
101
111
support traditional locking and complex transactions for a number of
0 commit comments