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

Mongodb

Mongodb Problem Overview


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

Mongodb Solutions


Solution 1 - Mongodb

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

Solution 2 - Mongodb

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

Solution 3 - Mongodb

db.stats()                in Bytes
db.stats(1024)            in KB
db.stats(1024*1024)       in MB
db.stats(1024*1024*1024)  in GB

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikeyView Question on Stackoverflow
Solution 1 - MongodbChris FulstowView Answer on Stackoverflow
Solution 2 - MongodbAndreas JungView Answer on Stackoverflow
Solution 3 - MongodbMitko KeckaroskiView Answer on Stackoverflow