I want to know how to update the document atomically. My Scenario is, I have a spring application (with 2 instances) polling from mongodb and update the documents. In some situations 2 instances are picking the same document and overwrite the other instance update. I have to prevent this overwrite operation.
When I google the solution, I saw findAndModify is doing atomic update. I just want to understand how findAndModify works when 2 instances are trying to update the same document at the same time. Please suggest me if there are any better solution.
Service one acquire the lock and change the status field so service2 will wait and eventually will not get this specific document as its status has changed. So this read and update is atomic.
Please let me know if you have any additional questions.
@Pavel_Duchovny ,
I have another usecase, but wanted to know till when does service2 wait before giving exception if service one has not released the lock?