Uploaded image for project: 'Node.js Driver'
  1. Node.js Driver
  2. NODE-574

Concurrency issues with findOneAndUpdate

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.0.28
    • Component/s: None
    • Labels:

      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

        1. callback.zip
          4.95 MB
        2. callback-profiling.zip
          6.08 MB
        3. logs.zip
          13.89 MB

            Assignee:
            christkv Christian Amor Kvalheim
            Reporter:
            annaguri Anna Guri
            Votes:
            0 Vote for this issue
            Watchers:
            12 Start watching this issue

              Created:
              Updated:
              Resolved: