This post will show you how to find the size of a document and the size of a collection of documents using the mongo shell, namely enron.json (a collection of emails).
Let’s cover some ground by examining the database that contains the email records by running the following command.
admin 0.000GB config 0.000GB enron 0.011GB local 0.000GB
Amongst the different databases shown above, we are only interested in
In the next step, we will try to get more information on the enron database so that we can access its collections and documents to get their sizes.
To do that, we will run the
use enron command to start working on the enron database.
> use enron
switched to db enron
Now, let’s see what collections exist in the enron database by running the command below.
> show collections
The output above tells us that emails is a collection in enron.
With this information, we can proceed to find the size of a single document in emails and the size of the entire emails collections.
MongoDB command to find the size of a single document in a collection
bsonsize returns the size in bytes of a given document and db.emails.findOne returns the first document in the emails collection.
MongoDB command to find the size of a collection of documents
We find the total size of the emails collection by running the command below.
> db. emails.totalSize()
The returned value of totalSize() is the “total size in bytes of the data in the collection plus the size of every index on the collection” (MongoDB Manual).
“The value returned is the sum of db.collection.storageSize() and db.collection.totalIndexSize() in bytes” (MongoDB Manual).
Alternatively, we can get the size of the data in emails using the following command:
"ns" : "enron.emails",
"size" : 15583945,
"count" : 5929,
"avgObjSize" : 2628,
"storageSize" : 11763712,