CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > How to > How to Check MongoDB Database size?

How to Check MongoDB Database size?

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: August 31, 2020 · 3 min read · 1 Comments
Share: X in 🔗
check MongoDb database size

Are you managing a MongoDB server on Ubuntu and need to monitor its disk usage? While MongoDB offers built-in tools for database analysis, a command-line approach provides a fast, reliable way to measure total storage consumption directly from your server. In this guide, you’ll learn how to check MongoDB’s storage usage in seconds—no complex queries required!

There are two ways to check MongoDB database Size:

  • Using the Command Line (Quick & Easy Method) for entire database.
  • Using db.stats() for single database.

Table of Contents

Using command line
Step-by-Step: Check MongoDB Storage Usage
Step 1: Locate Your MongoDB Data Directory
Step 2: Calculate Disk Usage
Why This Method Works Best
Troubleshooting Tips
Using db.stats()

Using command line

Before diving into the steps, let’s address why this method works:

  • Simplicity: No need to log into the MongoDB shell or run scripts.
  • Accuracy: Measures the entire data directory, including journals, temporary files, and hidden system collections.
  • Speed: Get results instantly with a single terminal command.

Step-by-Step: Check MongoDB Storage Usage

Step 1: Locate Your MongoDB Data Directory

MongoDB stores all database files in a dedicated directory. By default, this is /var/lib/mongodb, but the exact path depends on your configuration. To confirm:

  1. Open your MongoDB configuration file:
sudo cat /etc/mongod.conf
  1. Look for the storage.dbPath line. This specifies your data directory.

Example Output:

storage:
  dbPath: /var/lib/mongodb

Step 2: Calculate Disk Usage

Once you’ve identified the data directory, use the du (disk usage) command to measure its size:

sudo du -sh /var/lib/mongodb/
  • Flags Explained:
    • -s: Summarizes the total size (no per-file breakdown).
    • -h: Displays the result in a human-readable format (e.g., MB, GB).

Example Output:

1.3G  /var/lib/mongodb/

This shows your MongoDB data directory uses 1.3GB of disk space.

Why This Method Works Best

  1. Comprehensive Measurement: Unlike MongoDB’s sizeOnDisk metric (which excludes journals and temp files), this method accounts for every file in the data directory.
  2. No Authentication Needed: Perfect for environments where MongoDB shell access is restricted.
  3. Universal Compatibility: Works for all MongoDB deployments (standalone, replica sets, or sharded clusters).

Troubleshooting Tips

  • Permission Issues: Use sudo to ensure you have read access to the data directory.
  • Custom Configurations: If you’ve customized your dbPath, replace /var/lib/mongodb with your directory.

Using db.stats()

  1. Login to MongoDb database
  2. Switch to the database with use command. Eg: use database_name
  3. Now, we can run the command: db.stats()
  4. The command shows the details of the database:
database detail

5. We can see the database name zvls and the storageSize which is the actual used size in the database zvls. Hence, it is using the 1253376 bytes of the data and is equivalent to 1.25 mb.

Reference: https://docs.mongodb.com/manual/reference/command/dbStats/

Related Posts:

  • How to Upgrade AWS EC2 Ubuntu Version: A Step-by-Step Guide
  • Command Line Arguments in Java
  • Master Spring Data JPA Method Queries: The Ultimate…
  • How to Solve “systemctl command not found” Error: A…
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Read AWS Secrets Manager in Spring Boot…
Tags:devopsmongodb
Was this article helpful?
← Previous ArticleHow to export MongoDB data into CSV file?
Next Article →How to access MongoDb database from remote machine with authentication?

1 thought on “How to Check MongoDB Database size?”

  1. Stephen
    March 24, 2025 at 12:33 pm

    Thanks. It helped

    Log in to Reply

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap