Dushyant Bangal
Dushyant Bangal

Reputation: 6403

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.

Upvotes: 2

Views: 4136

Answers (2)

Marcus Lanvers
Marcus Lanvers

Reputation: 411

In addition to MrWhilihog's post, you can also get the data doing the following:

var db = mongoose.connection;
db.db.stats(function (err, stats) {
  console.log(stats);
});

This way you are able to get the stats later when your connection is already open.

Upvotes: 0

MrWillihog
MrWillihog

Reputation: 2646

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);
  });
});

Upvotes: 8

Related Questions