0

I want to keep track(by loging to another collection) on any Create , Update or Delete operation that happen in Mongodb.

My existing app (NodeJs) uses mongoose for repository. I'm trying to log every time something has changed(CUD) in the DB. Simple solution would be to add logs everywhere i modify to db, but i have so many methods.I wonder if i can "hook" on a post Save globally? something like middleware to Mongo.

UshaP
  • 1,271
  • 2
  • 18
  • 32

1 Answers1

1

Use below hook on schema on which you want to log(this is only for save function)

 schema.post('save', function(next){
        //log data using this here
        next();
        })

Refer to this link: http://mongoosejs.com/docs/middleware.html

as per this link:

> use myDb
switched to db myDb
> db.getProfilingLevel()
0
> db.setProfilingLevel(2)
{ "was" : 0, "slowms" : 1, "ok" : 1 }
> db.getProfilingLevel()
2
> db.system.profile.find().pretty()

reference: MongoDB logging all queries

Community
  • 1
  • 1
Sachin
  • 2,912
  • 16
  • 25
  • Thanks. That's actually very nice. I was thinking more towards using mongo optlog ... any thought about that? – UshaP Sep 30 '16 at 02:15
  • I don't know if there is any other way in mongodb to store particular operations but as I know that mongodb create logs for each operation happens on db. so you can log all queries. check update. – Sachin Sep 30 '16 at 05:13