title: Atomicity and CAS operations in MongoDB
body: <p>Currently I am trying to understand how to implement a CAS operation in mongodb properly to support optimistic locking. I've found out that updates in <code>mongodb</code> are atomic but I am not sure what this means (only the document rewrite is atomic or all cycle of update, including search for corresponding document and its rewriting, is atomic?). Lets consider following example. </p>
  
  <ol>
  <li>Some document exists in some collection with <code>_id</code> value set to 123 and has attribute <code>cas_val</code> set to 10.</li>
  <li>The first client wants to update <code>cas_val</code> of the document with <code>_id</code> equal to 123 to 11.</li>
  <li>The second client wants to update <code>cas_val</code> of the document with <code>_id</code> equal to 123 to 11. </li>
  <li>Both clients operate at the same time and operations can interleave.</li>
  </ol>
  
  <p>So, is it possible that <strong>both</strong> operation succeed in case when no other updates to the document with <code>_id</code> 123 were performed? </p>
  
  <p>P.S. Is there some build in technique in <code>mongodb</code> for the optimistic locking scenario?</p>