0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

2 Answers2

4

spring-data-mongodb provides AbstractMongoEventListener for 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 @Configuration or componentscan) which extends AbstractMongoEventListener and implement various abstract methods provided by AbstractMongoEventListener and write your audit logic inside these methods.

Take a look at LoggingEventListener under org.springframework.data.mongodb.core.mapping.event in your spring-data-mongodb jar.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
  • Thanks for the quick response. The thing is I don't want to work it on Spring. Instead, I'm trying to use plain Java due to some constraints. I tried one solution: Imported javassist for ProxyFactory, made proxy classes for DB and DBCollection to override the CRUD operations. Facing some issues due to Groovy's automatic type-casting feature. So, not moving forward with that approach. – Nimesh Nischal Feb 01 '18 at 13:29
  • Definitely. Did you happen to go through my edited comment? – Nimesh Nischal Feb 01 '18 at 13:36
  • Why would you want to reinvent the wheel. If spring is already doing it and you are using spring, so why not use the functionality. It will be really messy if you want to do it using proxy and reflection – pvpkiran Feb 01 '18 at 13:39
  • Not trying to reinvent the wheel, nor trying to use it. A lot of my services operating on mongodb are running as POJO, so cannot use spring features to audit them. That's the issue – Nimesh Nischal Feb 01 '18 at 13:46
1

I think you can try CommandListener. You just need new a object that implement the interface and add it to MongoClientOptions, then use the MongoClientOptions create a MongoClient. like that:

MongoCommandListener listener = new MongoCommandListener();

MongoClientOptions options =
                        MongoClientOptions.builder().addCommandListener(listener).build();

return new MongoClient(new ServerAddress(host,port), options);
Eric Liu
  • 11
  • 1
  • Thank you. It helped me a lot! I'm using GMongo, whose latest version 1.5, has mongo-java-driver dependency of version 2.13.0, in which MongoClientOptions does not support addCommandListener yet. – Nimesh Nischal Mar 22 '18 at 09:46
  • Tried it, it's working good as far. The listener is logging the details of the command before the query and on query success. But, as I need to audit, I need the changed documennts before & after the query. Is there any way to get the document in the listener methods? – Nimesh Nischal Mar 23 '18 at 10:01
  • 1
    Oh...if you want to changed documents before & after the query,this way is not work. – Eric Liu Mar 23 '18 at 15:53
  • I guessed that too.. Thank you for helping! – Nimesh Nischal Mar 26 '18 at 05:04