Docs Menu

sh.listShards()

sh.listShards()

sh.listShards() returns a list of the configured shards in a sharded cluster. This information matches the shards field returned by the listShards database command.

sh.listShards() has the following syntax:

sh.listShards()

The output for sh.listShards() returns an array of documents, each describing one shard. Each document may contain the following fields:

Field
Description

_id

Name of the shard.

host

Hostname of the shard. If the shard is a replica set, host lists the hostname of each mongod instance in the replica set.

draining

If true, indicates that removeShard has been called for this shard and it is in the process of being drained.

tags

List of zones to which the shard belongs.

state

Internal field used during the addShard sequence to ensure that all steps and configuration necessary to add the shard to the cluster are completed.

The following code runs sh.listShards() and provides an example output array:

sh.listShards()
[
{
"_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
}
]