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.
show dbs
Output:
admin 0.000GB
config 0.000GB
enron 0.011GB
local 0.000GB
Amongst the different databases shown above, we are only interested in enron
.
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
Output:
switched to db enron
Now, let’s see what collections exist in the enron database by running the command below.
> show collections
Output:
emails
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.
> Object.bsonsize(db.emails.findOne())
Output:
4634
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()
Output:
11833344
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:
> db.emails.dataSize()
Output:
11833344
Or.
> db.emails.stats()
Partial output:
{
"ns" : "enron.emails",
"size" : 15583945,
"count" : 5929,
"avgObjSize" : 2628,
"storageSize" : 11763712,
....
References
https://docs.mongodb.com/manual/reference/method/db.collection.totalSize/