Docs Menu

Docs HomeMongoDB Manual


On this page

  • Definition
  • Syntax
  • Considerations
  • Examples

Adds a shard replica set to a sharded cluster.


In mongosh, this command can also be run through the sh.addShard() helper method.

Helper methods are convenient for mongosh users, but they may not return the same level of information as database commands. In cases where the convenience is not needed or the additional return fields are required, use the database command.

The command has the following syntax:

addShard: "<replica_set>/<hostname><:port>",
name: "<shard_name>"


Starting in version 6.2, MongoDB removes the maxSize field from the addShard command. As a result:

  • Running addShard with the maxSize field returns an InvalidOptions error.

  • New documents in the shards collection no longer include the maxSize field.

  • Any pre-existing maxSize field entries are ignored.

The command contains the following fields:


The replica set name, hostname, and port of at least one member of the shard's replica set. Any additional replica set member hostnames must be comma separated. For example:

<replica_set>/<hostname><:port>,<hostname><:port>, ...
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.

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 Balancer Internals for more information.


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

The following command adds a replica set as a shard:

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


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

←  abortReshardCollectionaddShardToZone →
Share Feedback
© 2023 MongoDB, Inc.


  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2023 MongoDB, Inc.