On this page本页内容
dbStats
The dbStats
command returns storage statistics for a given database. dbStats
命令返回给定数据库的存储统计信息。The command has the following syntax:该命令具有以下语法:
db.runCommand( { dbStats: 1, scale: <number>, freeStorage: 0 } )
The dbStats
command takes the following fields:dbStats
命令接受以下字段:
dbStats | 1 |
scale |
|
freeStorage |
|
In 在mongosh
, the db.stats()
function provides a wrapper around dbStats
.mongosh
中,db.stats()
函数提供了dbStats
的包装器。
The time required to run the command depends on the total size of the database. 运行该命令所需的时间取决于数据库的总大小。Because the command must touch all data files, the command may take several seconds to run.由于该命令必须接触所有数据文件,因此该命令可能需要几秒钟才能运行。
After an unclean shutdown of a 在使用Wired Tiger存储引擎的mongod
using the Wired Tiger storage engine, count and size statistics reported by dbStats
may be inaccurate.mongod
不干净地关闭后,dbStats
报告的计数和大小统计数据可能不准确。
The amount of drift depends on the number of insert, update, or delete operations performed between the last checkpoint and the unclean shutdown. 漂移量取决于在最后一个检查点和不干净关闭之间执行的插入、更新或删除操作的数量。Checkpoints usually occur every 60 seconds. 检查点通常每60秒出现一次。However, 然而,使用非默认mongod
instances running with non-default --syncdelay
settings may have more or less frequent checkpoints.--syncdelay
设置运行的mongod
实例可能或多或少有检查点。
Run 在validate
on each collection on the mongod
to restore statistics after an unclean shutdown.mongod
上的每个集合上运行validate
,以在不干净的关闭后恢复统计数据。
After an unclean shutdown:不干净停机后:
Starting in MongoDB 4.4, to run on a replica set member, 从MongoDB 4.4开始,要在副本集成员上运行,dbStats
operations require the member to be in PRIMARY
or SECONDARY
state. dbStats
操作要求成员处于PRIMARY
或SECONDARY
状态。If the member is in another state, such as 如果成员处于其他状态,如STARTUP2
, the operation errors.STARTUP2
,则操作将出错。
In previous versions, the operations can also be run when the member is in 在以前的版本中,也可以在成员处于STARTUP2
. STARTUP2
时运行操作。However, the operations wait until the member transitions to 但是,操作将等待成员转换到RECOVERING
.RECOVERING
。
dbStats.objects
Number of objects (specifically, documents) in the database across all collections.所有集合中数据库中的对象(特别是文档)数量。
dbStats.avgObjSize
Average size of each document in bytes. 每个文档的平均大小(字节)。This is the 这是dataSize
divided by the number of documents. dataSize
除以文档数。The scale argument does not affect the scale参数不影响avgObjSize
value.avgObjSize
值。
dbStats.dataSize
Total size of the uncompressed data held in the database. 数据库中保存的未压缩数据的总大小。The 删除文档时,dataSize
decreases when you remove documents.dataSize
会减小。
For databases using the WiredTiger storage engine, 对于使用WiredTiger存储引擎的数据库,如果启用压缩,dataSize
may be larger than storageSize
if compression is enabled. dataSize
可能大于storageSize
。The 文档收缩时,dataSize
decreases when documents shrink.dataSize
会减小。
dbStats.storageSize
Sum of the space allocated to all collections in the database for document storage, including free space.分配给数据库中用于文档存储的所有集合的空间总和,包括可用空间。
The storageSize
does not decrease as you remove or shrink documents. storageSize
不会随着删除或收缩文档而减小。This value may be smaller than 对于使用启用压缩的WiredTiger存储引擎的数据库,此值可能小于dataSize
for databases using the WiredTiger storage engine with compression enabled.dataSize
。
storageSize
does not include space allocated to indexes. 不包括分配给索引的空间。See 有关总索引大小,请参阅indexSize
for the total index size.indexSize
。
dbStats.freeStorageSize
Sum of the free space allocated to all collections in the database for document storage. 分配给数据库中用于文档存储的所有集合的可用空间的总和。Free database storage space is allocated to the collection but does not contain data.已将可用数据inventory
储空间分配给集合,但不包含数据。
freeStorageSize
does not include free space allocated to indexes. 不包括分配给索引的可用空间。See 请参阅indexFreeStorageSize
for the total free index size.indexFreeStorageSize
以了解总可用索引大小。
To include this value in the 要在dbStats
output, set freeStorage to 1.dbStats
输出中包含此值,请将freeStorage
设置为1。
Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。
dbStats.indexSize
Sum of the space allocated to all indexes in the database, including free index space.分配给数据库中所有索引的空间总和,包括可用索引空间。
dbStats.indexFreeStorageSize
Sum of the free space allocated to all indexes in the database. 分配给数据库中所有索引的可用空间的总和。Free database storage space is allocated to the index but does not contain data.可用数据inventory
储空间分配给索引,但不包含数据。
indexFreeStorageSize
does not include free space allocated to document storage. 不包括分配给文档存储的可用空间。See 请参阅freeStorageSize
for the total free document storage size.freeStorageSize
以了解可用文档存储的总大小。
To include this value in the 要在dbStats
output, set freeStorage to 1.dbStats
输出中包含此值,请将freeStorage
设置为1。
Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。
dbStats.totalSize
Sum of the space allocated for both documents and indexes in all collections in the database. 为数据库中所有集合中的文档和索引分配的空间总和。Includes used and free storage space. 包括已用和可用存储空间。This is the sum of 这是storageSize
and indexSize
.storageSize
和indexSize
的总和。
New in version 4.4.在版本4.4中新增。
dbStats.totalFreeStorageSize
Sum of the free storage space allocated for both documents and indexes in all collections in the database. 为数据库中所有集合中的文档和索引分配的可用存储空间的总和。This is the sum of 这是freeStorageSize
and indexFreeStorageSize
.freeStorageSize
和indexFreeStorageSize
的总和。
To include this value in the 要在dbStats
output, set freeStorage to 1.dbStats
输出中包含此值,请将freeStorage
设置为1。
Updated in version 5.3.0, 5.2.1, and 5.0.6.在版本5.3.0、5.2.1和5.0.6中更新。
dbStats.scaleFactor
scale
value used by the command.命令使用的值。
If you specified a non-integer scale factor, MongoDB uses the integer part of the specified factor. 如果指定了非整数比例因子,MongoDB将使用指定因子的整数部分。For example, if you specify a scale factor of 例如,如果指定比例因子1023.999
, MongoDB uses 1023
as the scale factor.1023.999
,MongoDB将使用1023
作为比例因子。
New in version 4.2.在版本4.2中新增。
The following examples demonstrate 以下示例演示了dbStats
usage.dbStats
的用法。
To limit the data returned to a single field, append the field name to the 要将返回的数据限制为单个字段,请将字段名附加到dbStats
command. dbStats
命令。This example returns the 此示例返回indexSize
value:indexSize
值:
db.runCommand( { dbStats: 1 } ).indexSize
To view free storage usage, set freeStorage to 1.要查看可用存储使用情况,请将freeStorage
设置为1。
db.runCommand( { dbStats: 1, scale: 1024, freeStorage: 1 } )
Example output:示例输出:
{ db: 'test', collections: 2, views: 0, objects: 1689, avgObjSize: 52.56542332741267, dataSize: 86.7021484375, storageSize: 100, freeStorageSize: 32, indexes: 2, indexSize: 116, indexFreeStorageSize: 36, totalSize: 216, totalFreeStorageSize: 68, scaleFactor: 1024, fsUsedSize: 60155820, fsTotalSize: 61255492, ok: 1, '$clusterTime': { clusterTime: Timestamp({ t: 1646085664, i: 1 }), signature: { hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), keyId: Long("0") } }, operationTime: Timestamp({ t: 1646085664, i: 1 }) }
The freeStorage
field enables the collection and display of the highlighted metrics.freeStorage
字段用于集合和显示突出显示的度量。
The scale field sets the displayed values to kilobytes.scale字段将显示的值设置为千字节。