How can we monitor mongoDB queries? How can we monitor mongoDB queries? mongoose mongoose

How can we monitor mongoDB queries?


Mongoose out-of-the-box supports only basic debug:

mongoose.set('debug', true);

But that doesn't measure query time so is almost no use for profiling. Since mongoose 4.* you can use middleware to measure request time:http://mongoosejs.com/docs/middleware.html

There are some nodejs libs to measure execution time of different code blocks and app performance:


The number of read (query, getmore) and write (insert, delete, update) operations are reported in opcounters under the serverStatus command.Remember that you should also correlate these throughput statistics along with resource saturation metrics such as currentQueue.readers and currentQueue.writers (also part of serverStatus).

Here are detailed all the different ways to collect the metrics you need: using Utilities, Commands, or monitoring tools integrating with MongoDB (in the same series you will also find all the statistics you need to properly monitor MongoDB).


What you need is debugging mode:

All executed collection methods will log output of their arguments to your console

mongoose.set('debug', true);

Or you could add callback as third argument that allows you get additional info:

mongoose.set('debug', function (collection, method, query, doc [, options]) {    console.log(/* your log format */);});

MongoDB also provides monitoring of your mongod server in a cloud with MMS.

EDIT: to save your queries in csv you could use csv-write-stream module with the following example:

var csvWriter = require('csv-write-stream');var fs = require('fs');var writer = csvWriter();// create write stream to `queries.csv` file.writer.pipe(fs.createWriteStream('queries.csv'));mongoose.set('debug', function (collection, method, query, doc [, options]) {    writer.write({collection: collection, method: method, query: query, doc: JSON.stringify(doc)});   });// close stream on mongoose disconnectedmongoose.connection.on('disconnected', function () {      writer.end();});