发布于 2015-09-14 14:45:43 | 255 次阅读 | 评论: 0 | 来源: 网络整理

Overview

Write operations are atomic on the level of a single document: no single write operation can atomically affect more than one document or more than one collection.

When a single write operation modifies multiple documents, the operation as a whole is not atomic, and other operations may interleave. The modification of a single document, or record, is always atomic, even if the write operation modifies multiple sub-document within the single record.

No other operations are atomic; however, you can isolate a single write operation that affects multiple documents using the isolation operator.

This document describes one method of updating documents only if the local copy of the document reflects the current state of the document in the database. In addition the following methods provide a way to manage isolated sequences of operations:

Update if Current

In this pattern, you will:

  • query for a document,
  • modify the fields in that document
  • and update the fields of a document only if the fields have not changed in the collection since the query.

Consider the following example in JavaScript which attempts to update the qty field of a document in the products collection:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
var myCollection = db.products;
var myDocument = myCollection.findOne( { sku: 'abc123' } );

if (myDocument) {

   var oldQty = myDocument.qty;

   if (myDocument.qty < 10) {
       myDocument.qty *= 4;
   } else if ( myDocument.qty < 20 ) {
       myDocument.qty *= 3;
   } else {
       myDocument.qty *= 2;
   }

   myCollection.update(
      {
        _id: myDocument._id,
        qty: oldQty
      },
      {
        $set: { qty: myDocument.qty }
      }
   )

   var err = db.getLastErrorObj();

   if ( err && err.code ) {
       print("unexpected error updating document: " + tojson( err ));
   } else if ( err.n == 0 ) {
       print("No update: no matching document for { _id: " + myDocument._id + ", qty: " + oldQty + " }")
   }

}

Your application may require some modifications of this pattern, such as:

  • Use the entire document as the query in lines 18 and 19, to generalize the operation and guarantee that the original document was not modified, rather than ensuring that as single field was not changed.
  • Add a version variable to the document that applications increment upon each update operation to the documents. Use this version variable in the query expression. You must be able to ensure that all clients that connect to your database obey this constraint.
  • Use $set in the update expression to modify only your fields and prevent overriding other fields.
  • Use one of the methods described in 创建一个自动递增的序列字段.
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务