Mongo save not returning Duplicate key error

Hi,

I’m trying to do a save on Mongo while doing a create. I want to throw an error if Duplicate key is found (this is a composite key for me). When I try to save the data with same composite key, somehow it works and does not throw error which is weird.

I did follow steps mentioned here: https://www.baeldung.com/spring-data-mongodb-composite-key#1-testing-our-model

Can someone help to understand what might be happening here?

Hi @Pranita_Hatte and welcome to the MongoDB Community forums. :wave:

MongoDB should not be saving documents that violate a unique constraint. Can you share the index definition that has this constraint on it and then the documents that were inserted that have the same keys?

1 Like

I was able to understand this. Mongo save is actually just updating the data and not doing the unique id check. I am also using CoroutineCrudRepository.