Search code examples
springspring-data-mongodbevent-listener

Why does Spring Data MongoDB not expose events for update…(…) methods?


It appears that the update for mongoOperations do not trigger the events in AbstractMongoEventListener.

This post indicates that was at least the case in Nov 2014

Is there currently any way to listen to update events like below? This seems to be quite a big omission if it is the case.

MongoTemplate.updateMulti()

Thanks!


Solution

  • This is no oversight. Events are designed around the lifecycle of a domain object or a document at least, which means they usually contain an instance of the domain object you're interested in.

    Updates on the other hand are completely handled in the database. So there are no documents or even domain objects handled in MongoTemplate. Consider this basically the same way JPA @EntityListeners are only triggered for entities that are loaded into the persistence context in the first place, but not triggered when a query is executed as the execution of the query is happening in the database.