Concurrency issues with findOneAndUpdate

1,030 views
Skip to first unread message

guri...@gmail.com

unread,
Sep 26, 2015, 7:54:01 PM9/26/15
to mongodb-user, Anna Guri
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.  
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' (when 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

Thanks 
Anna

Stephen Steneker

unread,
Sep 28, 2015, 6:33:13 PM9/28/15
to mongodb-user
Hi Anna,

I noticed you've also posted this to Jira as a possible bug (NODE-574) and there is ongoing investigation if anyone would like to watch the issue there.

Regards,
Stephen

Asya Kamsky

unread,
Sep 29, 2015, 1:56:37 AM9/29/15
to mongodb-user
As Stennie said, the investigation should continue in Jira ticket, however I do want to point out that there is no MongoDB API called findOneAndModify (or findOneAndModify) - that seems to be a Mongoose thing - and under the hood it's probably using findAndModify thought Node.js driver.

Asya


--
You received this message because you are subscribed to the Google Groups "mongodb-user"
group.
 
For other MongoDB technical support options, see: http://www.mongodb.org/about/support/.
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
To post to this group, send email to mongod...@googlegroups.com.
Visit this group at http://groups.google.com/group/mongodb-user.
To view this discussion on the web visit https://groups.google.com/d/msgid/mongodb-user/5e5134be-e2fe-4149-86b4-1027b97d36d7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asya Kamsky
Lead Product Manager
MongoDB
Download MongoDB - mongodb.org/downloads
Free MongoDB Monitoring - cloud.mongodb.com
Free Online Education - university.mongodb.com
Get Involved - mongodb.org/community
We're Hiring! - https://www.mongodb.com/careers

guri...@gmail.com

unread,
Oct 1, 2015, 6:14:31 AM10/1/15
to mongodb-user
That's correct, I opened a JIRA ticket and working with the team to provide the requested data for further investigation. 
The issue happens in  Node.js when using findOneAndUpdate API.

Thanks
Anna

guri...@gmail.com

unread,
Oct 1, 2015, 6:19:38 AM10/1/15
to mongodb-user
Thanks Stephen, I'll use the JIRA ticker for further investigation. 
Reply all
Reply to author
Forward
0 new messages