db.stats()
On this page本页内容
Description描述
db.stats(scale)
-
Returns statistics that reflect the use state of a single database.返回反映单个数据库的使用状态的统计信息。Thedb.stats()
method is a wrapper around thedbStats
database command.db.stats()
方法是dbStats
数据库命令的包装器。
Parameters参数
The db.stats()
method has the following optional parameters:db.stats()
方法具有以下可选参数:
scale | number | 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 的scale 值。1023.999 , MongoDB uses 1023 as the scale factor.1023.999 ,MongoDB将使用1023 作为比例因子。scaleFactor used to scale the size values. scaleFactor 。 |
freeStorage | number | freeStorage to 1.freeStorage 设置为1。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 })
}
Behavior行为
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 db.stats()
may be inaccurate.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副本集成员状态限制
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 also run when the member is in 在以前的版本中,成员处于STARTUP2
. The operations wait until the member transitioned to RECOVERING
.STARTUP2
时也会运行这些操作。操作将等待,直到成员转换到RECOVERING
。
Examples实例
Scale Output Values缩放输出值
To to return values in kilobytes, set the scale to 要返回以千字节为单位的值,请将1024
:scale
设置为1024
:
db.stats(1024)
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.stat()
中。
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.stat()
。
The following example returns the 以下示例以千字节为单位返回indexFreeStorageSize
in kilobytes:indexFreeStorageSize
:
db.stats( { freeStorage: 1, scale: 1024 } ).indexFreeStorageSize