Docs Menu

Docs HomeDevelop ApplicationsMongoDB Manual

Add Shards to a Cluster

On this page

  • Considerations
  • Add a Shard to a Cluster

You add shards to a sharded cluster after you create the cluster or any time that you need to add capacity to the cluster. If you have not created a sharded cluster, see Deploy a Sharded Cluster.

In production environments, all shards should be replica sets.

When you add a shard to a sharded cluster, you affect the balance of chunks among the shards of a cluster for all existing sharded collections. The balancer will begin migrating chunks so that the cluster will achieve balance. See Cluster Balancer for more information.

Chunk migrations can have an impact on disk space, as the source shard automatically archives the migrated documents by default. For details, see moveChunk directory.

When adding a shard to a cluster, always ensure that the cluster has enough capacity to support the migration required for balancing the cluster without affecting legitimate production traffic.

You interact with a sharded cluster by connecting to a mongos instance.

  1. In mongosh, connect to the mongos instance. For example, if a mongos is accessible at on port 27017, issue the following command:

    mongosh --host --port 27017
  2. Add a shard to the cluster using the sh.addShard() method, as shown in the examples below. Issue sh.addShard() separately for each shard. If the shard is a replica set, specify the name of the replica set and specify a member of the set. In production deployments, all shards should be replica sets.



    You can instead use the addShard database command, which lets you specify a name and maximum size for the shard. If you do not specify these, MongoDB automatically assigns a name and maximum size. To use the database command, see addShard.

    The following are examples of adding a shard with sh.addShard():

    • To add a replica set shard named rs1 with a member running on port 27018 on, issue the following command:

      sh.addShard( "rs1/" )
    • To add a standalone mongod shard running on port 27018 of, issue the following command:

      sh.addShard( "" )


    It might take some time for chunks to migrate to the new shard.

←  Migrate a Sharded Cluster to Different HardwareRemove Shards from an Existing Sharded Cluster →