Does the MongoDB stats() function return bits or bytes?
MongodbMongodb 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