I'm new to Spring and MongoDB. I'm trying to audit the CRUD operations on MongoDB. I've found many plugins that audit the changes on MongoDB level, but I'm looking for something like an interceptor or hook (for example, EmptyInterceptor for hibernate) which works on Java level.
I'm using GMongo on Java to operate on MongoDB. What is something which can help me in this matter?
spring-data-mongodb provides
AbstractMongoEventListenerfor exactly these purposes.Do read the complete Lifecycle events here.
All you have got to do is write a class (make sure Spring scans it, using
@Configurationor componentscan) which extendsAbstractMongoEventListenerand implement various abstract methods provided by AbstractMongoEventListener and write your audit logic inside these methods.Take a look at
LoggingEventListenerunder org.springframework.data.mongodb.core.mapping.event in your spring-data-mongodb jar.