---
title: Collect Mongo DB Collection and Index Sizes Across All Databases
slug: user-guide-10x/collect-mongo-db-collection-and-index-sizes-across-all-databases
docTags: 
createdAt: 2025-12-10T09:43:20.123Z
---



This article explains how to collect MongoDB size statistics for all collections and indexes across all databases. The output is sorted by largest collections first and can be saved to a file for review.

This is useful when investigating disk usage growth, storage exhaustion, or identifying which databases and collections are consuming the most space.

## Overview

The script does the following:

1. Loops through all databases
2. Retrieves stats for every collection
3. Captures collection size, storage size, index size, and total size
4. Sorts the results from largest to smallest by total size
5. Prints the output in a readable format

## Notes

**size**

Logical uncompressed size of the documents in the collection

**storageSize**

Physical space allocated for document storage

**totalIndexSize**

Total size of all indexes for the collection

**totalSize**

Combined size of collection storage and indexes

Because Swimlane uses WiredTiger, collection data is compressed. As a result, storageSize may be smaller than size.

## Steps

1. Connect to the Mongo shell from the Mongo pod.

Example:

:::BlockQuote
export NS=default
:::

:::BlockQuote
kubectl -n $NS exec -i mongo-0 -- mongosh --quiet \\
-u Admin \\
-p "$(kubectl -n $NS get secret mongo-admin -n default -o jsonpath='&#x7B;.data.password}' | base64 -d)" \\
\--authenticationDatabase admin \\
\--tls \\
\--tlsAllowInvalidHostnames \\
\--tlsAllowInvalidCertificates \\
admin
:::

2. Run the following script:

:::BlockQuote
function getReadableFileSizeString(fileSizeInBytes) \{
&#x20;   var i = -1;
&#x20;   var byteUnits = \[' kB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
&#x20;   do \{
&#x20;       fileSizeInBytes = fileSizeInBytes / 1024;
&#x20;       i++;
&#x20;   } while (fileSizeInBytes > 1024);

&#x20;   return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits\[i];
}

var dbs = db.getMongo().getDBNames();
var results = \[];

dbs.forEach(function(dbName) \{
&#x20;   var currentDB = db.getMongo().getDB(dbName);

&#x20;   currentDB.getCollectionNames().forEach(function(collName) \{
&#x20;       try \{
&#x20;           var s = currentDB.getCollection(collName).stats();

&#x20;           results.push(\{
&#x20;               ns: s.ns,
&#x20;               count: s.count || 0,
&#x20;               size: s.size || 0,
&#x20;               storageSize: s.storageSize || 0,
&#x20;               totalIndexSize: s.totalIndexSize || 0,
&#x20;               totalSize: (s.storageSize || 0) + (s.totalIndexSize || 0)
&#x20;           });
&#x20;       } catch (e) \{
&#x20;           print("Skipping " + dbName + "." + collName + ": " + e);
&#x20;       }
&#x20;   });
});

results.sort(function(a, b) \{
&#x20;   return b.totalSize - a.totalSize;
});

results.forEach(function(r) \{
&#x20;   print(
&#x20;       r.ns +
&#x20;       " | count=" + r.count +
&#x20;       " | size=" + getReadableFileSizeString(r.size) +
&#x20;       " | storageSize=" + getReadableFileSizeString(r.storageSize) +
&#x20;       " | totalIndexSize=" + getReadableFileSizeString(r.totalIndexSize) +
&#x20;       " | totalSize=" + getReadableFileSizeString(r.totalSize)
&#x20;   );
});
:::

3. Review the output.

Example output:

:::BlockQuote
app.records | count=250000 | size=5.2 GB | storageSize=2.8 GB | totalIndexSize=1.4 GB | totalSize=4.2 GB
app.attachments | count=18000 | size=3.6 GB | storageSize=2.1 GB | totalIndexSize=512.0 MB | totalSize=2.6 GB
:::

**Save output to a file**

If you want to save the output to a file, run the command below from the shell instead of entering mongosh interactively:

:::BlockQuote
export NS=default
:::

:::BlockQuote
OUTPUT\_FILE="mongo\_collection\_index\_sizes\_$(date +%Y%m%d\_%H%M%S).txt"

kubectl -n $NS exec -i mongo-0 -- mongosh --quiet \\
-u Admin \\
-p "$(kubectl -n $NS get secret mongo-admin -n default -o jsonpath='&#x7B;.data.password}' | base64 -d)" \\
\--authenticationDatabase admin \\
\--tls \\
\--tlsAllowInvalidHostnames \\
\--tlsAllowInvalidCertificates \\
admin \<\<'EOF' > "$OUTPUT\_FILE"
function getReadableFileSizeString(fileSizeInBytes) \{
&#x20;   var i = -1;
&#x20;   var byteUnits = \[' kB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB'];
&#x20;   do \{
&#x20;       fileSizeInBytes = fileSizeInBytes / 1024;
&#x20;       i++;
&#x20;   } while (fileSizeInBytes > 1024);

&#x20;   return Math.max(fileSizeInBytes, 0.1).toFixed(1) + byteUnits\[i];
}

var dbs = db.getMongo().getDBNames();
var results = \[];

dbs.forEach(function(dbName) \{
&#x20;   var currentDB = db.getMongo().getDB(dbName);

&#x20;   currentDB.getCollectionNames().forEach(function(collName) \{
&#x20;       try \{
&#x20;           var s = currentDB.getCollection(collName).stats();

&#x20;           results.push(\{
&#x20;               ns: s.ns,
&#x20;               count: s.count || 0,
&#x20;               size: s.size || 0,
&#x20;               storageSize: s.storageSize || 0,
&#x20;               totalIndexSize: s.totalIndexSize || 0,
&#x20;               totalSize: (s.storageSize || 0) + (s.totalIndexSize || 0)
&#x20;           });
&#x20;       } catch (e) \{
&#x20;           print("Skipping " + dbName + "." + collName + ": " + e);
&#x20;       }
&#x20;   });
});

results.sort(function(a, b) \{
&#x20;   return b.totalSize - a.totalSize;
});

results.forEach(function(r) \{
&#x20;   print(
&#x20;       r.ns +
&#x20;       " | count=" + r.count +
&#x20;       " | size=" + getReadableFileSizeString(r.size) +
&#x20;       " | storageSize=" + getReadableFileSizeString(r.storageSize) +
&#x20;       " | totalIndexSize=" + getReadableFileSizeString(r.totalIndexSize) +
&#x20;       " | totalSize=" + getReadableFileSizeString(r.totalSize)
&#x20;   );
});
EOF

echo "Output saved to $OUTPUT\_FILE"
:::

**How to interpret the results**

Use `totalSize` as the best quick view of how much space a collection and its indexes are consuming together.

Large `size` with smaller `storageSize`

This usually indicates document compression.

Large `totalIndexSize`

This suggests indexes are contributing significantly to disk usage.

Large `storageSize` with relatively small count

This may indicate larger document payloads or previously allocated space.

**Use this process when:**

1. MongoDB disk usage is growing unexpectedly
2. The Mongo volume or mount point is nearing full capacity
3. You need to identify the largest collections across the environment
4. You want to compare document growth versus index growth

**Additional recommendation**

If disk usage is the main concern, collect this output along with:

1. df -h
2. lsblk
3. sudo fdisk -l

This helps correlate MongoDB collection growth with underlying disk and partition usage.

