Skip to content
Advertisement

Change or override default Behavior of Mongorepository.save(document)

Is there any way to intercept or change the document before MongoRepository.save(e) updates it?

I am trying to push a subproperty inside and array in a document. I have tried to manipulate DBObjects by implementing converter(using custom converter) but the $push operation did not work over there.

I think to make it work I have to implement something like mongoOperation.update(dbObjectMatch,dbObjectUdate).

I found MongoRepositorysave(document) doesn’t support partial update, i.e write only the change to an existing document. I want to know the internal code of MongoRepository.save to override the default behavior.

I have implemented MyRepositoryCustom where I can override save by extending the same in MyRepository, which extends MongoRepository<T, ID extends Serializable> and then used mongoTemplate.updateFirst(query,update,Clazz.class) to achieve what I am looking for but I am not satisfied.

Advertisement

Answer

You have multiple, slightly different questions:

From your title:

Change/Override Default Behaviour of Mongorepository Save() ( S save(S var1)) Method

You can use custom implementations to override the behavior of existing methods in Spring Data repositories. See the reference documentation how to do that. Your last paragraph suggests you already do that. Unfortunately you don’t tell us why you aren’t satisfied with this.

Is there any way to intercept/change before Mongorepository Save() ( S save(S var1)) method For document update.

Yes a Spring Data MongoDB repository fires various life cycle events for this purpose. Once again, see the reference documentation for details.

I want to know the Internal code of Mongorepository Save

What you are looking for is SimpleMongoRepository.java which delegates for almost all work to MongoTemplate.java

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement