Database Manual / Reference / Database Commands / Sharding

listShards (database command数据库命令)

Definition定义

listShards
The listShards command returns a list of the configured shards in a sharded cluster. listShards命令返回分片集群中已配置分片的列表。listShards is only available on mongos instances and must be issued against the admin database.listShards仅在mongos实例上可用,必须针对admin数据库发布。

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部署的完全托管服务
  • 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(
{
listShards: 1
}
)

Example示例

The following operation runs listShards against the mongos admin database:以下操作对mongos管理数据库运行listShards

db.adminCommand({ listShards: 1 })

The following document is an example of the output from a listShards command:以下文档是listShards命令输出的示例:

{
"shards": [
{
"_id": "shard01",
"host": "shard01/host1:27018,host2:27018,host3:27018",
"state": 1
},
{
"_id": "shard02",
"host": "shard02/host4:27018,host5:27018,host6:27018",
"tags": [ "NYC" ],
"state": 1
},
{
"_id": "shard03",
"host": "shard03/host7:27018,host8:27018,host9:27018",
"state": 1
}
],
"ok": 1,
"$clusterTime" : {
"clusterTime" : Timestamp(1510716515, 1),
"signature" : {
"hash" : BinData(0,"B2ViX7XLzFLS5Fl9XEuFXbwKIM4="),
"keyId" : Long("6488045157173166092")
}
},
"operationTime" : Timestamp(1510716515, 1)
}

Output输出

listShards returns a document that includes:返回一个文档,其中包括:

  • A shards field which contains an array of documents, each describing one shard. Each document may contain the following fields:一个包含文档数组的shards字段,每个文档描述一个分片。每份文档可能包含以下字段:

    Field字段Description描述
    _idName of the shard.分片的名称。
    hostHostname of the shard. If the shard is a replica set, host lists the hostname of each mongod instance in the replica set.分片的主机名。如果分片是一个副本集,host会列出副本集中每个mongod实例的主机名。
    drainingIf true, indicates that removeShard (database command) has been called for this shard and it is in the process of being drained.如果为true,则表示已为此分片调用removeShard(数据库命令),并且该分片正在被耗尽。
    tagsList of zones to which the shard belongs.分片所属的区域列表。
    stateInternal field used during the addShard (database command) sequence to ensure that all steps and configuration necessary to add the shard to the cluster are completed.addShard(数据库命令)序列中使用的内部字段,用于确保将分片添加到集群所需的所有步骤和配置都已完成。
  • The ok status field, the operationTime field, and the $clusterTime field for the operation. 操作的ok状态字段、operationTime字段和$clusterTime字段。For details on these fields, see Response.有关这些字段的详细信息,请参阅响应