-
Type: Task
-
Resolution: Done
-
Priority: Critical - P2
-
None
-
Affects Version/s: 2.0.28
-
Component/s: None
-
Labels:
-
Empty show more show less
Our Node.js application uses findOneAndUpdate API in order to update a document.
Per MongoDB documentation findOneAndModify is atomic API and "update document if current" approach should avoid data inconsistency or conflicts.
http://docs.mongodb.org/manual/tutorial/update-if-current/
I've noticed that findOneAndUpdate not always behaves as an atomic API.
The scenario is as follows:
The application retrieves a document from MongoDB using the following query:
Collection.find({"Posted" : { $exists: false} }, queryOptions)
Thereafter, it calls the findOneAndUpdate API to set the 'Posted' field.
Collection.findOneAndUpdate( {"SId" : eventDoc.SId, "SequenceNumber": eventDoc.SequenceNumber, "Posted" : { $exists: false}}, {$set : {"Posted" : "processing"}, {returnOriginal: false}, function(updtErr, updtEventDoc) { if (updtErr) { // handle error } else if (updtEventDoc.value == null) { // document was updated by another process } else { // document has been modified successfully } } );
I expect the update to modify the document only if the 'Posted' field was not set by another process since the find query.
However, sometimes the same document is updated successfully by multiple processes - findOneAndUpdate returns the modified document with the 'Posted' field set to 'processing' (I expect only one process to get the updated document while others get 'null').
Another issue I see with the API is that the callback function is never called (although, the document is updated in DB).
The issue is intermittent.
Is that a known issue?
MongoDB version is 2.6.9
- is related to
-
SERVER-21280 document was updated by two processes simultaneously
- Closed