log all queries that mongoose fire in the application

codeofnode picture codeofnode · Sep 12, 2013 · Viewed 49.3k times · Source

I have application using nodejs and mongodb. I have used mongoose for ODM. Now i want to log all the queries that mongoose fire during the whole application.

How to log these?

Answer

mr.freeze picture mr.freeze · Sep 12, 2013

You can enable debug mode like so:

mongoose.set('debug', true);

or add your own debug callback:

mongoose.set('debug', function (coll, method, query, doc [, options]) {
 //do your thing
});

This will log all executed collection methods and their arguments to the console.