This version of the documentation is archived and no longer supported.


On this page



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

Run addShard when connected to a mongos instance. The command takes the following form when adding a single database instance as a shard:

{ addShard: "<hostname><:port>", maxSize: <size>, name: "<shard_name>" }

When adding a replica set as a shard, use the following form:

{ addShard: "<replica_set>/<hostname><:port>", maxSize: <size>, name: "<shard_name>" }

The command contains the following fields:

Field Type Description
addShard string The hostname and port of the mongod instance to be added as a shard. To add 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.

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.


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.