Adds either a database instance or a replica set to a sharded cluster. The optimal configuration is to deploy shards across replica sets.

To run addShard, use the db.runCommand( { <command> } ) method.

Run addShard when connected to a mongos instance.

The command takes the following form:

Changed in version 3.6.

   addShard: "<hostname><:port> | <replica_set>/<hostname><:port>",
   maxSize: <size>,
   name: "<shard_name>",
   txnNumber: <long>,
   lsid: { id: <UUID> }

The command contains the following fields:

Field Type Description
addShard string

To add a standalone mongod instance as a shard, specify the hostname and port of the instance:


To a replica set as a shard, specify the name of the replica set and the hostname and port of a member of the replica set.

maxSize integer Optional. The maximum size in megabytes of the shard. If you set maxSize to 0, MongoDB does not limit the size of the shard.
name string Optional. A name for the shard. If this is not specified, MongoDB automatically provides a unique name.
txnNumber 64-bit integer/long


A strictly increasing non-negative number that uniquely identifies the command in the command’s session.

If specified, the command must also include the lsid option.

New in version 3.6.

lsid document


The document that specifies the unique id of the session associated with the command. If the txnNumber is specified, the lsid is required.


The mongo shell and the drivers provide helpers to start the session and set the lsid for the commands.

New in version 3.6.

The addShard command stores shard configuration information in the config database. Always run addShard when using the admin database.

Specify a maxSize when you have machines with different disk capacities, or if you want to limit the amount of data on some shards. The maxSize constraint prevents the balancer from migrating chunks to the shard when the value of mem.mapped exceeds the value of maxSize.



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.

Changed in version 2.6: Chunk migrations can have an impact on disk space. Starting in MongoDB 2.6, the source shard automatically archives the migrated documents by default. For details, see moveChunk directory.

Hidden Members


You cannot include a hidden member in the seed list provided to addShard.


The following command adds the database instance running on port 27027 on the host as a shard:

use admin
db.runCommand({addShard: ""})


Do not use localhost for the hostname unless your configuration server is also running on localhost.

The following command adds a replica set as a shard:

use admin
db.runCommand( { addShard: "repl0/"} )

You may specify all members in the replica set. All additional hostnames must be members of the same replica set.