How to identify FindAndModify inserted or updated the document

Hi Team

We are using mongoTemplate.findAndModify for our atomic insertion.
Now we face situation on what actions findAndModify did, whether the insert / update operation.

Since findAndModify return the newly inserted or updated document How to identify the document is created newly or updated.

or is there any other option for atomic insertion? we are encountering situation where 3 or more threads are trying to insert the document at the same time. we don’t want the duplicate key exception also, if the document already exists gracefully skips the insertion.

Hi @Prabaharan_Kathiresa,

The option new in findAndModify can be set to true if you want to return the newest version of the document.

A try/catch in your code should be able to handle the duplicate key exceptions but as this operation is atomic, this shouldn’t happen I guess if you are matching / filtering on _id.