Venkat Loganathan
Venkat Loganathan

Reputation: 95

Get Collections stats in MongoDB 3.2

In earlier versions say MongoDB 2.6, the DBCollection class has this method getStats().

DBCollection.getStats()

In the new 3.x versions , we have a new class

MongoCollection

and it has no method to get the statistics.

My question is how to get the statistics from the MongoCollection class

Upvotes: 3

Views: 698

Answers (1)

Hayden Braxton
Hayden Braxton

Reputation: 1161

So I think I've found a solution for you. It's a bit hackish, but from what I was reading, I couldn't find any other way around it. I was reading resources from Mongo and they were saying they simplifed the driver a bit and reduced the amount of available methods for a collection. I would guess that getStats() probably got cut since it doesn't seem like something you would do often, at least not programmatically for most use cases anyways. So here's what you can do:

First, a MongoDatabase object will have a runCommand() method. 3.0 driver docs

If you look here, you will get a list of all the commands you can execute with runCommand().

One of those commands is collStats. Based on the documentation, it looks like you will want to pass run command a Bson object that has the following form:

{
   collStats: <string>,
   scale: <int>,
   verbose: <boolean>
}

where the collStats is the string name of the collection for which you want stats. Scale is an optional field; you can read about it at the last link. Verbose defaults to false.

I don't know for sure that this will get you want you want, but it will at least get you pretty close. Let me know how it works out!

Upvotes: 2

Related Questions