Definition定义
dbStatsThedbStatscommand 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:该命令包含以下字段:
dbStats | 1 |
scale |
|
freeStorage |
|
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 使用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:不干净停机后:
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操作要求该成员处于PRIMARY或SECONDARY状态。If the member is in another state, such as 如果成员处于另一种状态,如STARTUP2, the operation errors.STARTUP2,则操作错误。
Output输出
dbStats.dbName of the database.数据库的名称。
dbStats.collectionsNumber of collections in the database.数据库中的集合数。
dbStats.viewsNumber of views in the database.数据库中的视图数。
dbStats.objectsNumber of objects (specifically, documents) in the database across all collections.数据库中所有集合中的对象(特别是文档)数量。
dbStats.avgObjSizeAverage size of each document in bytes. This is the每个文档的平均大小(字节)。这是dataSizedivided by the number of documents. The scale argument does not affect theavgObjSizevalue.dataSize除以文档数量。scale参数不影响avgObjSize值。
dbStats.dataSizeTotal size of the uncompressed data held in the database. The数据库中保存的未压缩数据的总大小。删除文档时,dataSizedecreases when you remove documents.dataSize会减小。For databases using the WiredTiger storage engine,对于使用WiredTiger存储引擎的数据库,如果启用了压缩,则dataSizemay be larger thanstorageSizeif compression is enabled. ThedataSizedecreases when documents shrink.dataSize可能会大于storageSize。文档收缩时,dataSize会减小。
dbStats.storageSizeSum of the disk space allocated to all collections in the database for document storage, including free space.分配给数据库中所有集合用于文档存储的磁盘空间总和,包括可用空间。The删除或收缩文档时,storageSizedoes not decrease as you remove or shrink documents.storageSize不会减小。This value may be smaller than对于使用启用压缩的WiredTiger存储引擎的数据库,此值可能小于dataSizefor databases using the WiredTiger storage engine with compression enabled.dataSize。storageSizedoes not include space allocated to indexes. See不包括分配给索引的空间。有关总索引大小,请参阅indexSizefor the total index size.indexSize。
dbStats.freeStorageSizeSum 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.分配给数据库中所有集合用于文档存储的可用空间之和。可用数据库存储空间已分配给集合,但不包含数据。freeStorageSizedoes not include free space allocated to indexes. See不包括分配给索引的可用空间。有关总可用索引大小,请参阅indexFreeStorageSizefor the total free index size.indexFreeStorageSize。To include this value in the要在dbStatsoutput, 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.indexesTotal number of indexes across all collections in the database.数据库中所有集合的索引总数。
dbStats.indexSizeSum 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在_ìdfield (no secondary indexes), the index size appears as zero because the collection does not require a separate index file._ìd字段上只有默认索引(没有辅助索引)的集群集合中,索引大小显示为零,因为该集合不需要单独的索引文件。
dbStats.indexFreeStorageSizeSum 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储空间已分配给索引,但不包含数据。indexFreeStorageSizedoes not include free space allocated to document storage. See不包括分配给文档存储的可用空间。请参阅freeStorageSizefor the total free document storage size.freeStorageSize以了解可用文档存储的总大小。indexFreeStorageSizedoes not include in-progress index builds.不包括正在进行的索引构建。To include this value in the要在dbStatsoutput, 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.totalSizeSum 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数据库中所有集合中为文档和索引分配的磁盘空间之和。包括已使用和免费存储空间。这是storageSizeandindexSize.storageSize和indexSize的总和。
dbStats.totalFreeStorageSizeSum of the free storage space allocated for both documents and indexes in all collections in the database. This is the sum of数据库中所有集合中为文档和索引分配的可用存储空间的总和。这是freeStorageSizeandindexFreeStorageSize.freeStorageSize和indexFreeStorageSize的总和。To include this value in the要在dbStatsoutput, 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命令使用的scalevalue 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如果指定了非整数比例因子,MongoDB将使用指定因子的整数部分。例如,如果指定比例因子1023.999, MongoDB uses1023as the scale factor.1023.999,MongoDB将使用1023作为比例因子。
dbStats.fsUsedSizeTotal size of all disk space in use on the filesystem where MongoDB stores data.MongoDB存储数据的文件系统上正在使用的所有磁盘空间的总大小。Tip
dbStats.fsTotalSizeTotal 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 } ).indexSizeView 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字段将显示的值设置为千字节。