You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB数据库已使用空间占比计算方法咨询

Understanding db.stats() for Calculating MongoDB Database Space Usage

Great question—this is such a common gotcha with MongoDB's db.stats() output, so you're not alone here! Let's break down what each key field actually means, why your initial calculation is giving values over 100%, and how to get the right space usage metrics you need.

First: Clarify What Each db.stats() Field Represents

Let’s start with the definitions you might have mixed up:

  • dataSize: This is the total size of all your documents in their uncompressed, raw BSON format. It doesn't account for MongoDB's storage optimizations like compression, preallocated space, or leftover "fragment" space from deleted/updated documents. It also doesn't include index size.
  • storageSize: This is the actual disk space used by your database's data collections (not indexes). It includes compressed data (if you have compression enabled), plus any fragment space from deleted documents. It does not include index storage.
  • indexSize: The total disk space used by all your database's indexes, again accounting for compression and fragments.
  • fileSize: The total size of all disk files allocated to your database (including both data and index files). This includes preallocated empty space that MongoDB reserves to avoid frequent disk IO as your data grows.

Why Your Initial Calculation Gives >100%

You mentioned using (dataSize/totalSize)*100—I assume you meant storageSize instead of totalSize? Either way, here's the issue:
If you have storage compression enabled (which is default for WiredTiger, MongoDB's default storage engine), storageSize is the compressed size of your data. Since compressed data is smaller than the raw uncompressed dataSize, dividing dataSize by storageSize will naturally give a value over 100%. This isn't an error—it just means your compression is working effectively!

How to Calculate the Right Space Usage Ratios

Depending on what you're trying to measure, here are the correct formulas:

1. Compression Efficiency (Raw Data vs. Compressed Storage)

If you want to see how well MongoDB is compressing your data:

(dataSize / storageSize) * 100

A value over 100% is good—higher means better compression.

2. Actual Used Space vs. Total Allocated Disk Space

If you want to know what percentage of the disk space you've allocated to MongoDB is actually being used (including data, indexes, and fragments):
First calculate total used space (data + indexes):

totalUsed = storageSize + indexSize

Then compute the ratio:

(totalUsed / fileSize) * 100

This tells you how much of the preallocated disk files are being utilized; the rest is empty preallocated space.

3. Effective Data Usage (Excluding Fragments)

If you're dealing with lots of document deletes/updates, storageSize will include unused fragment space. To see how much space your actual data (no fragments) takes up relative to allocated space:

  • For individual collections, use db.collection.stats() and look at the size field (this is the fragment-free data size).
  • Sum the size of all collections, add indexSize, then divide by fileSize:
    const totalEffectiveData = db.getCollectionNames().reduce((sum, coll) => sum + db[coll].stats().size, 0);
    const effectiveUsageRatio = ((totalEffectiveData + db.stats().indexSize) / db.stats().fileSize) * 100;
    
    This gives you a more accurate view of how much space your actual data is using, ignoring leftover fragments.

Quick Notes

  • Preallocated Space: MongoDB preallocates data files to avoid frequent disk writes, so fileSize will often be larger than totalUsed—this is normal, especially for new databases.
  • Fragment Cleanup: If storageSize is much larger than dataSize (without compression), you have significant fragmentation. In low-traffic windows, you can use db.repairDatabase() (locks the database, requires extra disk space) or db.runCommand({ compact: "yourCollectionName" }) to reclaim this space.

内容的提问来源于stack exchange,提问作者Milos

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:47:58