Enables sharding on the specified database. Once you enabled sharding for a database, you can use
sh.shardCollection()to shard collections in that database. The
This page documents a
mongoshmethod. This is not the documentation for database commands or language-specific drivers, such as Node.js.
For the database command, see the
For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.
For the legacy
mongoshell documentation, refer to the documentation for the corresponding MongoDB Server release:
sh.enableSharding() has the following form:
sh.enableSharding( <database>, <primary shard> // Optional. Available starting in MongoDB 4.2.2 (and 4.0.14) )
sh.enableSharding() method takes the following parameter:
The database for which you wish to enable sharding.
Optional. The primary shard for the database; the primary shard contains the unsharded collection for this database. In general, rather than explicitly specifying the primary shard, it is recommended to let the balancer select the primary shard instead.
In general, you should not need to specify the primary shard. Allow the balancer to select the primary shard instead.
Available starting in MongoDB 4.2.2 (and 4.0.14)
In general, you should not need to specify the primaryShard in the command. Instead, allow the balancer to select the primary shard.
However, if you do specify the primary shard for a database and the database
is already enabled for sharding with a different primary shard, the
operation returns an error and the primary shard for the database
remains as before. To change the primary shard for a database, use the
movePrimary command instead.
The following example, run from a
sharding for the
To verify, run