Database Manual / Reference / Database Commands / Diagnostics

dbStats (database command数据库命令)

Definition定义

dbStats
The dbStats command returns storage statistics for a given database.dbStats命令返回给定数据库的存储统计信息。

Compatibility兼容性

This command is available in deployments hosted in the following environments:此命令在以下环境中托管的部署中可用:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud:云中MongoDB部署的完全托管服务

Important

This command has limited support in M0 and Flex clusters. For more information, see Unsupported Commands.此命令在M0和Flex集群中的支持有限。有关详细信息,请参阅不支持的命令

  • MongoDB Enterprise: The subscription-based, self-managed version of MongoDB:MongoDB的基于订阅的自我管理版本
  • MongoDB Community: The source-available, free-to-use, and self-managed version of MongoDB:MongoDB的源代码可用、免费使用和自我管理版本

Syntax语法

The command has the following syntax:该命令具有以下语法:

db.runCommand(
{
dbStats: 1,
scale: <number>,
freeStorage: 0
}
)

Command Fields命令字段

The command takes the following fields:该命令包含以下字段:

Fields字段Description描述
dbStats1
scale

Optional. 可选。The scale factor for the various size data. The scale defaults to 1 to return size data in bytes. To display kilobytes rather than bytes, specify a scale value of 1024.各种大小数据的比例因子。scale默认为1,以字节为单位返回大小数据。要显示千字节而不是字节,请指定1024scale值。

If you specify a non-integer scale factor, MongoDB uses the integer part of the specified factor. For example, if you specify a scale factor of 1023.999, MongoDB uses 1023 as the scale factor.如果指定非整数比例因子,MongoDB将使用指定因子的整数部分。例如,如果指定比例因子1023.999,MongoDB将使用1023作为比例因子。

Starting in version 4.2, the output includes the scaleFactor used to scale the size values.从4.2版本开始,输出包括用于缩放大小值的scaleFactor

freeStorage

Optional. 可选。To return details on free space allocated to collections, set freeStorage to 1.要返回分配给集合的可用空间的详细信息,请将freeStorage设置为1。

If the instance has a large number of collections or indexes, obtaining free space usage data may cause processing delays. 如果实例有大量的集合或索引,获取可用空间使用数据可能会导致处理延迟。To gather dbStats information without free space details, either set freeStorage to 0 or do not include the field.要集合没有可用空间详细信息的dbStats信息,请将freeStorage设置为0或不包含该字段。

In mongosh, the db.stats() function provides a wrapper around dbStats.mongosh中,db.stats()函数为dbstats提供了一个包装器。

Behavior行为

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.运行该命令所需的时间取决于数据库的总大小。由于该命令必须触及所有数据文件,因此该命令可能需要几秒钟才能运行。

Accuracy after Unexpected Shutdown意外停机后的准确性

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by dbStats may be inaccurate.使用Wired Tiger存储引擎不干净地关闭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:不干净停机后:

Replica Set Member State Restriction副本集成员状态限制

To run on a replica set member, dbStats operations require the member to be in PRIMARY or SECONDARY state. 要在副本集成员上运行,dbStats操作要求该成员处于PRIMARYSECONDARY状态。If the member is in another state, such as STARTUP2, the operation errors.如果成员处于另一种状态,如STARTUP2,则操作错误。

Output输出

dbStats.db
Name of the database.数据库的名称。
dbStats.collections
Number of collections in the database.数据库中的集合数。
dbStats.views
Number of views in the database.数据库中的视图数。
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. The scale argument does not affect the avgObjSize value.每个文档的平均大小(字节)。这是dataSize除以文档数量。scale参数不影响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, dataSize may be larger than storageSize if compression is enabled. The dataSize decreases when documents shrink.对于使用WiredTiger存储引擎的数据库,如果启用了压缩,则dataSize可能会大于storageSize。文档收缩时,dataSize会减小。

dbStats.storageSize

Sum of the disk 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 dataSize for databases using the WiredTiger storage engine with compression enabled.对于使用启用压缩的WiredTiger存储引擎的数据库,此值可能小于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.分配给数据库中所有集合用于文档存储的可用空间之和。可用数据库存储空间已分配给集合,但不包含数据。

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.indexes
Total number of indexes across all collections in the database.数据库中所有集合的索引总数。
dbStats.indexSize

Sum of the disk space allocated to all indexes in the database, including free index space.分配给数据库中所有索引的磁盘空间之和,包括可用索引空间。

Note

In clustered collections with only a default index on the _ìd field (no secondary indexes), the index size appears as zero because the collection does not require a separate index file._ìd字段上只有默认索引(没有辅助索引)的集群集合中,索引大小显示为零,因为该集合不需要单独的索引文件。

dbStats.indexFreeStorageSize

Sum of the free disk 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以了解可用文档存储的总大小。

indexFreeStorageSize does not include in-progress index builds.不包括正在进行的索引构建。

To include this value in the dbStats output, set freeStorage to 1.要在dbStats输出中包含此值,请将freeStorage设置为1。

Updated in version 7.0, 6.3.2, 6.0.7, 5.3.0, 5.2.1, 5.0.19, and 5.0.6在7.0、6.3.2、6.0.7、5.3.0、5.2.1、5.0.19和5.0.6版本中更新

dbStats.totalSize
Sum of the disk 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.数据库中所有集合中为文档和索引分配的磁盘空间之和。包括已使用和免费存储空间。这是storageSizeindexSize的总和。
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.数据库中所有集合中为文档和索引分配的可用存储空间的总和。这是freeStorageSizeindexFreeStorageSize的总和。

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.命令使用的scale值。

If you specified a non-integer scale factor, MongoDB uses the integer part of the specified factor. For example, if you specify a scale factor of 1023.999, MongoDB uses 1023 as the scale factor.如果指定了非整数比例因子,MongoDB将使用指定因子的整数部分。例如,如果指定比例因子1023.999,MongoDB将使用1023作为比例因子。

dbStats.fsUsedSize

Total size of all disk space in use on the filesystem where MongoDB stores data.MongoDB存储数据的文件系统上正在使用的所有磁盘空间的总大小。

dbStats.fsTotalSize
Total size of all disk capacity on the filesystem where MongoDB stores data.MongoDB存储数据的文件系统上所有磁盘容量的总大小。

Examples示例

The following examples demonstrate dbStats usage.以下示例演示了dbStats的使用。

Limit Data Returned返回限制数据

To limit the data returned to a single field, append the field name to the dbStats command. This example returns the indexSize value:要将返回的数据限制为单个字段,请将字段名附加到dbStats命令。此示例返回indexSize值:

db.runCommand( { dbStats: 1 } ).indexSize

View Free Space Allocated to Collections查看分配给集合的可用空间

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字段将显示的值设置为千字节。