MongoDB data storage - Part 7
Every MongoDB instance consists of three things
Namespace file
Journal file
Data file.
And it could consist of multiple data files, multiple journal files and multiple namespaces even. Now, we’re interested in understanding the database storage, so we would like to know more about these data files, more about what constitutes a MongoDB. Fortunately, there are some commands that will do that for us, let’s take a look at a few of those.
First of all, I just want to know the size of the data files, using db.products.dataSize() – products is the name of our collection in pcat database.
Output information is in bytes, 2168 bytes.
Perhaps I’d like to know the storage size, and that’s going to include journal files as well. So db.products.storageSize(), should be bigger (16384 bytes), and in fact it is.
Now indexes can take up quite a bit of space, db.products.totalIndexSize(); this will include all indexes I have.
In above example, we don’t have any indexes created on the document, so it is showing as same as storage size. If we create indexes, the index size will be increased (as shown in below figure) equally distributed between all the indexes. I’ve created an index on the collection and you see the increase in number below.
If I’d like to know the complete size of everything, db.products.totalSize(),and you can see the size there.
If you want to see all the data together, and that’s easy to do using db.products.stats(). Now you should first of all notice that we’re seeing the same data size here, the same storage size here, the same total index size here, so I got all of that information with one simple command. But I’m also getting some other information, what’s the name of the namespace, what’s the count of objects or documents I have in there, what’s the average size of each document, how many indexes do I have, what are the sizes of each index, I’m getting lots of information here that can be very useful to me.
Now just as an interesting side note, you can notice the total index size in your screen is some number (in my case 32,768 bytes). We have two indexes each of 16384 bytes. If you add these two up, it will come to 32,768.
db.stats() is another command, will also give you statistics, but a different set.
Till now, all of these commands we issued gave us everything we wanted in bytes. That’s relatively simple to do by giving 1024 inside parenthesis, db.stats(1024)
The output lists the same statistics is in kilobytes. The output is a different set of statistics. It excludes the namespace but includes the numbers of collections and objects, and a higher total size and storage size.