storageSize() Function in MongoDB – Easy Guide

In this tutorial, I will explain how to use the storageSize() function in MongoDB.

The storageSize() function in MongoDB allows you to check how much storage size is allocated in bytes for a particular collection to store documents in it.

This guide will give you a simple demonstration of how to use the storageSize() function in MongoDB. So, let us get started.

storageSize() Function Syntax in MongoDB

Let us look at the syntax of the storageSize() function in MongoDB inside the mongo shell:

db.collection.storageSize()

Using the storageSize() Function in MongoDB

Let us get started with a demonstration of the usage of the storageSize() function in MongoDB inside the mongo shell to check the storage size allocation for a collection.

  • Run up the MongoDB local database server
  • Use the database you want by passing the below command inside your mongo shell:
show dbs
use droneStore
  • Let us now see what collections we have available in our database:
show tables

or

show collections
  • Let us now check the storage size allocated for our drones collection using the storageSize() function:
> db.drones.storageSize()
36864

We have successfully received the result in bytes that is 36864 bytes which is equivalent to 36.8 KBs.

Checking Storage Size Allocation for Compressed Collection Data

To check the storage size of a compressed collection data, which is the default for WiredTiger storage engine, you can make use of the dataSize() function in MongoDB. Remember, this function will return a size that is relatively smaller than what we receive from the storageSize() function output.

> db.drones.dataSize()
2035

The output we see here is very much smaller than the storageSize() function output.

Using stats() Output to Check Storage Size in MongoDB

Fortunately, Mongo also comes with another way where you can check the allocated storage size for your collection. We can make use of the stats() function in MongoDB.

Read in detail about the stats() function here.

Let us explore this function a bit:

> db.drones.stats()
{
        "ns" : "droneStore.drones",
        "size" : 2035,
        "count" : 5,
        "avgObjSize" : 407,
        "storageSize" : 36864,
        "freeStorageSize" : 16384,
        "capped" : false,
        "wiredTiger" : {
                "metadata" : {
                        "formatVersion" : 1
                },
                "creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1),assert=(commit_timestamp=none,durable_timestamp=none,read_timestamp=none,write_timestamp=off),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,import=

..... OUTPUT Cut off for demonstration .....

At the beginning of the output, you will see the storageSize property, which signifies the allocated storage size. This is most of the information of the storage size of the collection. You will find more information here, than from the storageSize() function.

Conclusion

Learn the different ways to use the storageSize() function in MongoDB and to grab the allocated storage size for your collection.

Noteworthy References

MongoDB Docs

Aneesha S
Aneesha S
Articles: 172