Dushyant Bangal Dushyant Bangal - 5 months ago 53
Node.js Question

mongoose db.stats() equivalent

I'm using mongoose, and I need to get stats of database.

I know about

YourModel.collection.stats()
, but thats just for a collection, I need similar thing, but for the database.

Please dont suggest running the shell command. I want to do it using mongoose.

Answer

You can call db.stats on the mongoose.connection object:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
  db.db.stats(function(err, stats) {
      console.log(stats);
  });
});
Comments