Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the MongoDB stats() function return bits or bytes?

Tags:

mongodb

When using MongoDB's .stats() function to determine document size, are the values returned in bits or bytes?

like image 604
Mikey Avatar asked May 21 '11 15:05

Mikey


People also ask

Which method returns statistics MongoDB?

Description. Returns statistics that reflect the use state of a single database. The db. stats() method is a wrapper around the dbStats database command.

What is stats in MongoDB?

stats() method is used to return a document that reports on the state of the current database. Syntax: db.stats(scale) Parameters: Name.

How does MongoDB calculate index size?

Get size of index and data in MongoDB collection with scaling factor. db. user. stats(1024) , is used to get size of data and size of index on user collection in MongoDB.

How do I find the size of a collection in MongoDB?

collection. totalSize() method is used to reports the total size of a collection, including the size of all documents and all indexes on a collection. Returns: The total size in bytes of the data in the collection plus the size of every index on the collection.


2 Answers

Running the collStats command - db.collection.stats() - returns all sizes in bytes, e.g.

> db.foo.stats() {     "size" : 715578011834,  // total size (bytes)     "avgObjSize" : 2862,    // average size (bytes) } 

However, if you want the results in another unit then you can also pass in a scale argument.

For example, to get the results in KB:

> db.foo.stats(1024) {     "size" : 698806652,  // total size (KB)     "avgObjSize" : 2,    // average size (KB) } 

Or for MB:

> db.foo.stats(1024 * 1024) {     "size" : 682428,    // total size (MB)     "avgObjSize" : 0,   // average size (MB) } 
like image 100
Chris Fulstow Avatar answered Sep 21 '22 09:09

Chris Fulstow


Bytes of course. Unless you pass in a scale as optional argument.

like image 43
Andreas Jung Avatar answered Sep 23 '22 09:09

Andreas Jung