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.