Database Manual / Reference / Database Commands / Diagnostics

dataSize (database command数据库命令)

Definition定义

dataSize
The dataSize command returns the size in bytes for the specified data.dataSize命令返回指定数据的大小(以字节为单位)。

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 is not supported 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(
{
dataSize: <string>,
keyPattern: <document>,
min: <document>,
max: <document>,
estimate: <boolean>
}
)

Command Fields命令字段

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

Field字段Type类型Description描述
dataSizestring字符串The name of the target collection.目标集合的名称。
keyPatterndocument文档

Optional. 可选。The collection's key pattern to examine.要检查的集合的关键模式。

The collection must have an index with the corresponding pattern. Otherwise dataSize returns an error message.集合必须具有具有相应模式的索引。否则,dataSize将返回错误消息。

mindocument文档Optional. 可选。The lower bound of the key range to be examined.要检查的关键范围的下限。
maxdocument文档Optional. 可选。The upper bound of the key range to be examined.要检查的关键范围的上限。
estimateboolean布尔值

Optional. 可选。When true, dataSize estimates the data size by assuming that all documents in the specified range are uniformly sized as per the collection's average object size. 当为true时,dataSize通过假设指定范围内的所有文档都按照集合的平均对象大小一致性大小来估计数据大小。The collection's average object size is obtained from the avgObjSize field in the output of the collStats command.集合的平均对象大小是从collStats命令输出中的avgObjSize字段获得的。

Defaults to false.默认为false

Example示例

The following operation runs the dataSize command on the database.collection collection, specifying a key pattern of {field: 1} with the lower bound of the range of keys to be examined being {field: 10} and the upper bound of the key to be examined being {field: 100}.以下操作在database.collection集合上运行dataSize命令,指定键模式为{field: 1},要检查的键范围的下限为{field: 10},要检测的键的上限为{field: 100}

db.runCommand({ dataSize: "database.collection", keyPattern: { field: 1 }, min: { field: 10 }, max: { field: 100 } })

This returns a document with the size in bytes for all matching documents. Replace database.collection with the database and collection from your deployment.这将返回一个文档,其中包含所有匹配文档的大小(以字节为单位)。用部署中的数据库和集合替换database.collection

The amount of time required to return dataSize depends on the amount of data in the collection.返回dataSize所需的时间取决于集合中的数据量。