Docs Menu
Docs Home
MongoDB Manual
/ / /


On this page

  • Definition
  • Compatibility
  • Syntax
  • Behavior
  • Example
sh.enableSharding(database, primaryShard)


Changed in version 6.0.

Starting in MongoDB 6.0, this method is not required to shard a collection.

Explicitly creates a database. Use the mongosh method sh.shardCollection() to shard collections on the database. The mongosh method sh.enableSharding() wraps the enableSharding command.


mongosh Method

This page documents a mongosh method. This is not the documentation for database commands or language-specific drivers, such as Node.js.

For the database command, see the enableSharding command.

For MongoDB API drivers, refer to the language-specific MongoDB driver documentation.

For the legacy mongo shell documentation, refer to the documentation for the corresponding MongoDB Server release:

mongo shell v4.4

This method is available in deployments hosted in the following environments:

  • MongoDB Atlas: The fully managed service for MongoDB deployments in the cloud


This command is not supported in serverless instances. For more information, see Unsupported Commands.

The sh.enableSharding() has the following form:

<primary shard> // Optional.

The sh.enableSharding() method takes the following parameter:


The name of the database you want to create.


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.

mongos uses "majority" for the enableSharding command and its helper sh.enableSharding().

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 mongos, explicitly creates the shardTest database:


To verify, run sh.status().


← sh.enableAutoSplit