Database Manual / Reference / mongosh Methods / Databases

db.stats() (mongosh method方法)

Description描述

db.stats(scale)

Returns statistics that reflect the use state of a single database.返回反映单个数据库使用状态的统计信息。

The db.stats() method is a wrapper around the dbStats database command.db.stats()方法是dbStats数据库命令的包装器。

Compatibility兼容性

This method 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的源代码可用、免费使用和自我管理版本

Parameters参数

The db.stats() method has the following optional parameters:db.stats()方法有以下可选参数:

Parameter参数Type类型Description描述
scalenumber数字

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,以字节为单位返回大小数据。要显示千字节而不是字节,请指定1024的刻度值。

If you specify 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作为比例因子。

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

freeStoragenumber数字

Optional. 可选。To return information 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 db.stats() data without free space details, either set freeStorage to 0 or do not include the parameter.要集合没有可用空间详细信息的db.stats()数据,请将freeStorage设置为0或不包含该参数。

Output输出

The db.stats() method returns a document with statistics about the database system's state. db.stats()方法返回一个包含数据库系统状态统计信息的文档A complete listing, including freeStorage details, resembles the following:包括freeStorage详细信息在内的完整列表如下:

{
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 })
}

For an explanation of the output, see Output.有关输出的说明,请参阅输出

Behavior行为

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

After an unclean shutdown of a mongod using the Wired Tiger storage engine, count and size statistics reported by db.stats() may be inaccurate.使用Wired Tiger存储引擎不干净地关闭mongod后,db.stats()报告的计数和大小统计数据可能不准确。

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,则操作错误。

Examples示例

Scale Output Values缩放输出值

To return values in kilobytes, set the scale to 1024:要返回以千字节为单位的值,请将scale设置为1024

db.stats(1024)

Note

The scale factor rounds values to whole numbers.比例因子将值四舍五入为整数。

Return a Single Value返回单个值

To return a single value, such as indexSize, append the field name to db.stats().要返回单个值,如indexSize,请将字段名附加到db.stats()

db.stats().indexSize
db.stats(1024).indexSize

The output shows the difference between the original and scaled values.输出显示了原始值和缩放值之间的差异。

118784
116

Return Information on Free Space Allocated to Collections返回分配给集合的可用空间信息

To return information on free space allocated to collections, pass the freeStorage parameter to db.stats().要返回分配给集合的可用空间信息,请将freeStorage参数传递给db.stats()

The following example returns the indexFreeStorageSize in kilobytes:以下示例以千字节为单位返回indexFreeStorageSize

db.stats( { freeStorage: 1, scale: 1024 } ).indexFreeStorageSize