In node.js, suppose you have a sequence of asynchronous calls to MongoDB such as:
collection.find(query, projection).toArray(function(err, result) {
var document = result[0];
// Do something based off of fields in document...
collection.update(...)(function(err, result) {
// Do something...
});
});
Will this entire block occur atomically? Or is interleaving possible when multiple calls are made to this method in quick succession?
My thoughts are: Since find is an asynchronous operation, it happens on some background thread. Therefore, a second call to this method may run the same find command and get the exact same result before the first call has a chance to update the document. The second call then has a stale reference to the document.
Is this correct or am I thinking about this all wrong?