How can I check the size of a database I have?
In the MongoHQ interface you will see a size next to each database and collection. This number is given by mongodb. You can open the mongodb console and get the size of a database by issuing the db.stats() command.
Here is some example output:
> db.stats()
{
"collections" : 3,
"objects" : 80614,
"dataSize" : 21069700,
"storageSize" : 39845376,
"numExtents" : 9,
"indexes" : 2,
"indexSize" : 6012928,
"ok" : 1
}
The important field to look at is storageSize. That number is in bytes so you will need to divide by 1024 - 1,048,576 or 1,073,741,824 for KB - MB or GB respectively.
Please Note: MongoDB adds padding into the files for insertion efficiency. So the database may report being full before it hits the exact number due to this.
http://www.mongodb.org/display/DOCS/Excessive+Disk+Space
Checking Size of a Collection
Use the validate command to check the size of a collection -- that is from the shell run:
> db.<collectionname>.validate(); > // these are faster: > db.<collectionname>.dataSize(); // just data size for collection > db.<collectionname>.storageSize(); // allocation size including unused space > db.<collectionname>.totalSize(); // data + index > db.<collectionname>.totalIndexSize(); // index data size
This command returns info on the collection data but note there is also data allocated for associated indexes. These can be checked with validate too, if one looks up the index's namespace name in the system.namespaces collection. For example:
> db.system.namespaces.find()
{"name" : "test.foo"}
{"name" : "test.system.indexes"}
{"name" : "test.foo.$_id_"}
> > db.foo.$_id_.validate()
{"ns" : "test.foo.$_id_" , "result" : "
validate
details: 0xb3590b68 ofs:83fb68
firstExtent:0:8100 ns:test.foo.$_id_
lastExtent:0:8100 ns:test.foo.$_id_
# extents:1
datasize?:8192 nrecords?:1 lastExtentSize:131072
padding:1
first extent:
loc:0:8100 xnext:null xprev:null
ns:test.foo.$_id_
size:131072 firstRecord:0:81b0 lastRecord:0:81b0
1 objects found, nobj:1
8208 bytes data w/headers
8192 bytes data wout/headers
deletedList: 0000000000001000000
deleted: n: 1 size: 122688
nIndexes:0
" , "ok" : 1 , "valid" : true , "lastExtentSize" : 131072}
Helpful scripts
These one-line scripts will print the stats for each db/collection:
db._adminCommand("listDatabases").databases.forEach(function (d) {mdb = db.getSiblingDB(d.name); printjson(mdb.stats())}) db._adminCommand("listDatabases").databases.forEach(function (d) {mdb = db.getSiblingDB(d.name); mdb.getCollectionNames().forEach(function(c) {s = mdb[c].stats(); printjson(s)})})

