MongoDB Storage Statistics (MMAPv1)

Overview

In MongoDB, the dbStats and collStats commands return storage statistics for your database and collections.

Below, follow the instructions for viewing the output of these commands directly in the mLab management portal or running the commands in the mongo shell.

We also discuss how MongoDB storage works and what some of size-related metrics actually mean.

Viewing the current statistics

To view the output of these commands in the mLab management portal (which also displays helpful definitions for each field):

  1. Log in to the mLab management portal
  2. From your account’s Home page, navigate to the deployment with the database whose stats you want to view
    • If necessary, navigate to the desired database after navigating to the deployment and if further desired, click the “Collections” tab to view stats for a specific collection
  3. Once you’ve navigated to the desired database or collection, click the “Stats” tab
    img-dbstats

To run these commands in the mongo shell, use the respective helper methods, both shown below:

> db.stats()

> db.<collection>.stats()

Understanding MongoDB storage and size metrics

MongoDB provides a variety of size-related metrics for your database and once you understand the basics of MongoDB storage, it’s easier to understand the differences between these metrics.

The basics: data files and extents

Every MongoDB instance consists of a namespace file, journal files and data files. Here, we’ll focus only on data files, since that is where all of the data and indexes for your database reside.

Data files store BSON documents, indexes, and MongoDB-generated metadata in structures called extents. Each data file is made up of multiple extents.

Extents are logical containers within data files used to store documents and indexes.

img-extents

The above diagram illustrates the relationship between data files and extents. Note:

The metrics: dataSize, storageSize, and fileSize

Some of the more commonly examined metrics from the dbStats command are dataSize, storageSize and fileSize. What is the difference between how these metrics are calculated?

Data size

img-datasize

The dataSize metric is the sum of the the sizes (in bytes) of all the documents and padding stored in the database.

While dataSize does decrease when you delete documents, it does not decrease when documents shrink because the space used by the original document has already been allocated (to that particular document) and cannot be used by other documents.

Alternatively, if a user updates a document with more data, dataSize will remain the same as long as the new document fits within its originally padded pre-allocated space.

Storage size

img-storagesize

The storageSize metric is equal to the size (in bytes) of all the data extents in the database. This number is larger than dataSize because it includes yet-unused space (in data extents) and space vacated by deleted or moved documents within extents.

The storageSize value does not decrease as you remove or shrink documents.

File size

img-filesize

The fileSize metric is equal to the size (in bytes) of all the data extents, index extents and yet-unused space (in data files) in the database. This metric represents the storage footprint of your database on disk. fileSize is larger than storageSize because it includes index extents and yet-unused space in data files.

fileSize does not decrease as you remove collections, documents or indexes. However, it will decrease if you drop a database or compact your deployment in order to de-fragment and release space back to the OS.

For further reading, MongoDB Inc. provides a technical explanation in its FAQ