Search code examples
mongodbspring-data-mongodbmongodb-atlas

MongoDB Change Streams behavior with Spring data save() method


We use MongoDB with Spring data MongoDB. We configured change streams on the database and plugged a Kafka source connector.

We noticed that we had many "replace" events. This can cause problems on the volume of data to process. In the MongoDB doc, it says :

A replace event occurs when an update operation removes a document from a collection and replaces it with a new document, such as when the replaceOne method is called. MongoDB doc.

However, in our code we don't use replace. From what I read in the docs and what I saw in the database oplog, the save() of spring does not do delta updates.

So, are the updates of the save() considered as replace by the MongoDB change streams?

Thanks.


Solution

  • The save() in the MongoTemplate class of Spring data MongoDB uses replaceOne when the save is considered as an update (id already filled in the entity).