Database Manual / Reference / mongosh Methods / Collections

db.collection.getShardDistribution() (mongosh method方法)

Definition定义

db.collection.getShardDistribution()

Important

mongosh Method方法

This page documents a mongosh method. This is not the documentation for a language-specific driver, such as Node.js.本页记录了一种mongosh方法。这不是针对特定语言驱动程序(如Node.js)的文档。

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.有关MongoDB API驱动程序,请参阅特定语言的MongoDB驱动程序文档

Prints the data distribution statistics for a sharded collection.打印分片集合的数据分布统计信息。

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 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 getShardDistribution() method has the following form:getShardDistribution()方法具有以下形式:

db.collection.getShardDistribution()

Output输出

Note

The behavior of getShardDistribution() changed in MongoDB Shell version 2.3.3:MongoDB Shell 2.3.3版本中getShardDistribution()的行为发生了变化:

  • Starting in MongoDB Shell version 2.3.3, getShardDistribution() only contains regular sharded data and does not account for orphaned documents.从MongoDB Shell 2.3.3版本开始,getShardDistribution()只包含常规分片数据,不考虑孤立文档
  • Prior to MongoDB Shell version 2.3.3, getShardDistribution() accounts for both regular sharded data and orphaned documents pending deletion. 在MongoDB Shell 2.3.3版本之前,getShardDistribution()既可以处理常规分片数据,也可以处理待删除的孤立文档。If the collection contains orphaned documents, getShardDistribution() might indicate that the collection is unbalanced even if the collection is balanced in terms of regular data. 如果集合包含孤立文档,即使集合在常规数据方面是平衡的,getShardDistribution()也可能表示集合是不平衡的。The shard containing orphaned data has more documents and greater data size, but the same number of chunks compared to other shards.包含孤立数据的分片具有更多的文档和更大的数据大小,但与其他分片相比,块的数量相同。

Sample Output示例输出

The following is a sample output for the distribution of a sharded collection:以下是分片集合分发的示例输出:

Shard shard01 at shard01/localhost:27018
{
data: '38.14MB',
docs: 1000003,
chunks: 2,
'estimated data per chunk': '19.07B',
'estimated docs per chunk': 500001
}
---
Shard shard02 at shard02/localhost:27019
{
data: '38.14B',
docs: 999999,
chunks: 3,
'estimated data per chunk': '12.71B',
'estimated docs per chunk': 333333
}
---
Totals
{
data: '76.29B',
docs: 2000002,
chunks: 5,
'Shard shard01': [ '50 % data', '50 % docs in cluster', '40B avg obj size on shard' ],
'Shard shard02': [ '49.99 % data', '49.99 % docs in cluster', '40B avg obj size on shard' ]
}

Output Fields输出字段

Shard shard01 at <host-a>   {
data: <size-a>,
docs: <count-a>,
chunks: <number of chunks-a>,
'estimated data per chunk': <size-a>/<number of chunks-a>,
'estimated docs per chunk': <count-a>/<number of chunks-a>
}
---
Shard shard02 at <host-b>
{
data: <size-b>,
docs: <count-b>,
chunks: <number of chunks-b>,
'estimated data per chunk': <size-b>/<number of chunks-b>,
'estimated docs per chunk': <count-b>/<number of chunks-b>
}
---
Totals
{
data: <stats.size>,
docs: <stats.count>,
chunks: <calc total chunks>,
Shard shard01: [ <estDataPercent-a> % data, <estDocPercent-a> % docs in cluster, stats.shards[ <shard-a> ].avgObjSize avg obj size on shard ],
Shard shard02: [ <estDataPercent-b> % data, <estDocPercent-b> % docs in cluster, stats.shards[ <shard-b> ].avgObjSize avg obj size on shard ]
}

The output information displays:输出信息显示:

Output输出Description描述
<shard-x>A string that holds the shard name.一个包含分片名称的字符串。
<host-x>A string that holds the host name(s).一个包含主机名的字符串。
<size-x>A number that includes the size of the data, including the unit of measure (e.g. b, Mb).包含数据大小的数字,包括度量单位(例如bMb)。
<count-x>A number that reports the number of documents in the shard.报告分片中文档数量的数字。
<number of chunks-x>A number that reports the number of chunks in the shard.一个报告分片中块数的数字。
<size-x>/<number of chunks-x>A calculated value that reflects the estimated data size per chunk for the shard, including the unit of measure (e.g. b, Mb).反映分片每个块的估计数据大小的计算值,包括度量单位(例如bMb)。
<count-x>/<number of chunks-x>A calculated value that reflects the estimated number of documents per chunk for the shard.一个计算值,反映了分片每个块的估计文档数。
<stats.size>A value that reports the total size of the data in the sharded collection, including the unit of measure.报告分片集合中数据总大小的值,包括度量单位。
<stats.count>A value that reports the total number of documents in the sharded collection.报告分片集合中文档总数的值。
<calc total chunks>

A calculated number that reports the number of chunks from all shards. For example:一个计算出的数字,报告所有分片中的块数。例如:

<calc total chunks> = <number of chunks-a> + <number of chunks-b>
<estDataPercent-x>

A calculated value that reflects, for each shard, the data size as the percentage of the collection's total data size. For example:一个计算值,反映了每个分片的数据大小占集合总数据大小的百分比。例如:

<estDataPercent-x> = <size-x>/<stats.size>
<estDocPercent-x>

A calculated value that reflects, for each shard, the number of documents as the percentage of the total number of documents for the collection. For example:一个计算值,反映了每个分片的文档数量占集合文档总数的百分比。例如:

<estDocPercent-x> = <count-x>/<stats.count>
stats.shards[ <shard-x> ].avgObjSizeA number that reflects the average object size, including the unit of measure, for the shard.一个反映分片平均对象大小的数字,包括度量单位。

Behavior行为

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

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. However, mongod instances running with non-default --syncdelay settings may have more or less frequent checkpoints.检查点通常每60秒出现一次。然而,使用非默认的--syncdelay设置运行的mongod实例可能或多或少地具有检查点。

Run validate on each collection on the mongod to restore statistics after an unclean shutdown.mongod上的每个集合上运行validate,以在不干净关闭后恢复统计数据。

After an unclean shutdown:不干净停机后: