JIYIK CN >

Current Location:Home > Learning > DATABASE > MongoDB >

Get the size of the database in MongoDB

Author:JIYIK Last Updated:2025/04/29 Views:

When working in MongoDB, do you know the size of your database? Today, we will learn how to get the size of a database in MongoDB using show dbsthe command and the method.db.stats()

Get the size of the database in MongoDB

We can show dbs;get the storage size of all databases in MongoDB using the command on the mongo shell as shown below.

Sample code:

> show dbs;

Output:

admin   0.000GB
config  0.000GB
local   0.000GB
test    0.001GB

The above output shows the database name and its size in GB. We use db.stats()the method to get detailed statistics about the database.

It has various fields that we need to understand first. Below is a brief description of each field, but you can find more detailed information about each field here .

Remember that we must db.stats()select a database before using the function.

Sample code:

> use test
> db.stats()

Output:

{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 15,
        "avgObjSize" : 99,
        "dataSize" : 1485,
        "storageSize" : 135168,
        "indexes" : 5,
        "indexSize" : 102400,
        "totalSize" : 237568,
        "scaleFactor" : 1,
        "fsUsedSize" : 176960913408,
        "fsTotalSize" : 208499617792,
        "ok" : 1
}

We can also pass the scale factor as per the project requirement.

See the following example as a demonstration.

Get statistics in bytes:

>db.stats()

Output:

{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 15,
        "avgObjSize" : 99,
        "dataSize" : 1485,
        "storageSize" : 135168,
        "indexes" : 5,
        "indexSize" : 102400,
        "totalSize" : 237568,
        "scaleFactor" : 1,
        "fsUsedSize" : 176986320896,
        "fsTotalSize" : 208499617792,
        "ok" : 1
}

Considering the above given storageSize, the database filesystemoccupies over 0.1352 MB. Similarly, we can get the size of the database in the following example.

Get statistics in KB:

> db.stats(1024)

Output:

{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 15,
        "avgObjSize" : 99,
        "dataSize" : 1.4501953125,
        "storageSize" : 132,
        "indexes" : 5,
        "indexSize" : 100,
        "totalSize" : 232,
        "scaleFactor" : 1024,
        "fsUsedSize" : 172839080,
        "fsTotalSize" : 203612908,
        "ok" : 1
}

Get statistics in MB:

> db.stats(1024*1024)

Output:

{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 15,
        "avgObjSize" : 99,
        "dataSize" : 0.0014162063598632812,
        "storageSize" : 0.12890625,
        "indexes" : 5,
        "indexSize" : 0.09765625,
        "totalSize" : 0.2265625,
        "scaleFactor" : 1048576,
        "fsUsedSize" : 168788.66796875,
        "fsTotalSize" : 198840.73046875,
        "ok" : 1
}

Get statistics in GB:

> db.stats(1024*1024*1024)

Output:

{
        "db" : "test",
        "collections" : 5,
        "views" : 0,
        "objects" : 15,
        "avgObjSize" : 99,
        "dataSize" : 0.0000013830140233039856,
        "storageSize" : 0.000125885009765625,
        "indexes" : 5,
        "indexSize" : 0.000095367431640625,
        "totalSize" : 0.00022125244140625,
        "scaleFactor" : 1073741824,
        "fsUsedSize" : 164.83288955688477,
        "fsTotalSize" : 194.18040084838867,
        "ok" : 1
}

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

List all collections in MongoDB Shell

Publish Date:2025/04/29 Views:156 Category:MongoDB

When using MongoDB , there are several ways to list the collections in the database. This article will discuss four different ways to get a list of collections in a MongoDB database. These methods are as follows: show collections List all c

Querying for non-null values in MongoDB

Publish Date:2025/04/29 Views:139 Category:MongoDB

This MongoDB article will explain how to query for non-null values ​​in MongoDB. To query for non-null values, you can use $ne the operator and $eq the operator and then specify the desired value to query. This article shows the readers

Shutting down with code:100 error in MongoDB

Publish Date:2025/04/29 Views:53 Category:MongoDB

This MongoDB tutorial will teach you to fix the error on different operating systems shutting down with code:100 . It will also talk about the root cause of why this issue occurs. shutting down with code:100 Errors in MongoDB As we all know

SELECT COUNT GROUP BY in MongoDB

Publish Date:2025/04/29 Views:74 Category:MongoDB

In this article, we will discuss the functions in MongoDB. Also, we will point out the aggregation functions in detail. We will explain in detail the different ways to count and sort multiple and single fields of Group in MongoDB. Operation

Differences between MongoDB and Mongoose

Publish Date:2025/04/29 Views:80 Category:MongoDB

This MongoDB article will discuss the differences between MongoDB and Mongoose. Unfortunately, most beginners tend to confuse these two concepts when they start developing applications and use MongoDB as their backend. MongoDB has its own s

Install MongoDB using Homebrew

Publish Date:2025/04/29 Views:161 Category:MongoDB

MongoDB is a well-known unstructured database management system that can handle large amounts of data. It is a document-oriented database system and belongs to the NoSQL family (non-SQL). Data and records are stored as documents that look a

Create a MongoDB dump of the database

Publish Date:2025/04/29 Views:62 Category:MongoDB

In this MongoDB article, you’ll get a walkthrough of Mongodump and Mongorestore , how to use them, and some simple examples of backing up and restoring your collections using both tools. mongodump Commands in MongoDB Mongodump is a tool t

Use find operator to join multiple conditions in MongoDB

Publish Date:2025/04/29 Views:132 Category:MongoDB

$lookup Today, we will see how to concatenate multiple conditions using the operator in MongoDB . In addition, we will explore some examples to demonstrate the use of $group the stage and $unionWidth the aggregation stage. $lookup Use the o

Date comparison in MongoDB

Publish Date:2025/04/28 Views:170 Category:MongoDB

This MongoDB tutorial discusses the issue of returning Date-based queries. In addition, there is also a good tutorial on how to use Date Range queries in MongoDB. Using date ranges in MongoDB We will learn to write MongoDB date range querie

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial