|
| 1 | +============================== |
| 2 | +Isolate Sequence of Operations |
| 3 | +============================== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +Background |
| 8 | +---------- |
| 9 | + |
| 10 | +Write operations are atomic on the level of a single document: no |
| 11 | +single write operation can atomically affect more than one document or |
| 12 | +more than one collection. |
| 13 | + |
| 14 | +When a single write operation modifies multiple documents, the |
| 15 | +operation as a whole is not atomic, and other operations may |
| 16 | +interleave. The modification of a single document, or record, is always |
| 17 | +atomic, even if the write operation modifies multiple sub-document |
| 18 | +*within* the single record. |
| 19 | + |
| 20 | +No other operations are atomic; however, you can *isolate* a |
| 21 | +single write operation that affects multiple documents using the |
| 22 | +:doc:`isolation operator </reference/operator/atomic>`. |
| 23 | + |
| 24 | +Additionally, the following patterns can manage a sequence of |
| 25 | +operations: |
| 26 | + |
| 27 | +- :method:`findAndModify() <db.collection.findAndModify()>` |
| 28 | + |
| 29 | +- :ref:`tutorial-atomic-update-if-current` |
| 30 | + |
| 31 | +- :doc:`/tutorial/perform-two-phase-commits` |
| 32 | + |
| 33 | +- :method:`ensureIndex() <db.collection.ensureIndex()>` to create a |
| 34 | + ``unique`` index on a field |
| 35 | + |
| 36 | +.. _tutorial-atomic-update-if-current: |
| 37 | + |
| 38 | +Update if Current |
| 39 | +----------------- |
| 40 | + |
| 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. |
| 45 | + |
| 46 | +Consider the following example which attempts to update the ``qty`` |
| 47 | +field of a document in the ``products`` collection: |
| 48 | + |
| 49 | +.. code-block:: javascript |
| 50 | + |
| 51 | + var myCollection = db.products; |
| 52 | + var myDocument = myCollection.findOne( { sku: 'abc123' } ); |
| 53 | + |
| 54 | + if (myDocument) { |
| 55 | + |
| 56 | + var oldQty = myDocument.qty; |
| 57 | + |
| 58 | + if (myDocument.qty < 10) { |
| 59 | + myDocument.qty *= 4; |
| 60 | + } else if ( myDocument.qty < 20 ) { |
| 61 | + myDocument.qty *= 3; |
| 62 | + } else { |
| 63 | + myDocument.qty *= 2; |
| 64 | + } |
| 65 | + |
| 66 | + myCollection.update( |
| 67 | + { |
| 68 | + _id: myDocument._id, |
| 69 | + qty: oldQty |
| 70 | + }, |
| 71 | + { |
| 72 | + $set: { qty: myDocument.qty } |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + var err = db.getLastErrorObj(); |
| 77 | + |
| 78 | + if ( err && err.code ) { |
| 79 | + print("unexpected error updating document: " + tojson( err )); |
| 80 | + } else if ( err.n == 0 ) { |
| 81 | + print("No update: no matching document for { _id: " + myDocument._id + ", qty: " + oldQty + " }") |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | +Consider the following modifications to the "Update if Current" strategy: |
| 87 | + |
| 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. |
| 91 | + |
| 92 | +- Add a version variable that is incremented upon each update operation |
| 93 | + to the documents. Use this version variable in the query expression. |
| 94 | + |
| 95 | +- Use :operator:`$set` in the update expression to modify only your |
| 96 | + fields and prevent overriding other fields. |
| 97 | + |
| 98 | +.. Add link to :doc:`/tutorial/create-an-auto-increment-field` once that branch is merged since it's a special case |
| 99 | + |
| 100 | +.. Maybe incorporate the blurb: "MongoDB does not |
| 101 | + support traditional locking and complex transactions for a number of |
| 102 | + reasons: First, in sharded environments, distributed locks could be |
| 103 | + expensive and slow. Mongo DB's goal is to be lightweight and fast. We |
| 104 | + dislike the concept of deadlocks. We want the system to be simple and |
| 105 | + predictable without these sort of surprises. We want Mongo DB to work |
| 106 | + well for realtime problems. If an operation may execute which locks |
| 107 | + large amounts of data, it might stop some small light queries for an |
| 108 | + extended period of time." |
0 commit comments