Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There may be other ways to accomplish this, but one approach is to version your documents, and issue updates against only the version that the user had previously read (i.e., ensure that no one else has updated the document since it was last read). Here's a brief example of this technique using pymongo:</p> <pre><code>&gt;&gt;&gt; db.foo.save({'_id': 'a', 'version': 1, 'things': []}, safe=True) 'a' &gt;&gt;&gt; db.foo.update({'_id': 'a', 'version': 1}, {'$push': {'things': 'thing1'}, '$inc': {'version': 1}}, safe=True) {'updatedExisting': True, 'connectionId': 112, 'ok': 1.0, 'err': None, 'n': 1} </code></pre> <p>note in the above, key "n" is 1, indicating that the document was updated</p> <pre><code>&gt;&gt;&gt; db.foo.update({'_id': 'a', 'version': 1}, {'$push': {'things': 'thing2'}, '$inc': {'version': 1}}, safe=True) {'updatedExisting': False, 'connectionId': 112, 'ok': 1.0, 'err': None, 'n': 0} </code></pre> <p>here where we tried to update against the wrong version, key "n" is 0</p> <pre><code>&gt;&gt;&gt; db.foo.update({'_id': 'a', 'version': 2}, {'$push': {'things': 'thing2'}, '$inc': {'version': 1}}, safe=True) {'updatedExisting': True, 'connectionId': 112, 'ok': 1.0, 'err': None, 'n': 1} &gt;&gt;&gt; db.foo.find_one() {'things': ['thing1', 'thing2'], '_id': 'a', 'version': 3} </code></pre> <p>Note that this technique relies on using safe writes, otherwise we don't get an acknowledgement indicating the number of documents updated. A variation on this would use the <a href="http://www.mongodb.org/display/DOCS/findAndModify+Command"><code>findAndModify</code></a> command, which will either return the document, or <code>None</code> (in Python) if no document matching the query was found. <code>findAndModify</code> allows you to return either the new (i.e. after updates are applied) or old version of the document.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload