Docs Menu

Docs HomeMongoDB Manual


On this page

  • Definition
  • Syntax
  • Command Fields
  • Behavior
  • Example


Changed in version 6.0.

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

The enableSharding command explicitly creates a database.


In mongosh, this command can also be run through the sh.enableSharding() 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:

  • Recommended. Specify the database to create:

    enableSharding: "<database name>"
  • Optionally, starting in MongoDB 4.2.2 (and 4.0.14), you can include the primary shard specification in the command, although this is not recommended:

    enableSharding: "<database name>",
    primaryShard: "<shard name>" // Available starting in MongoDB 4.2.2 (and 4.0.14)

You can only run the enableSharding command on the admin database from a mongos instance.

The command takes the following fields:


The database you want to use. The database is created if it does not exist.


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)

The command returns a document that contains status of the operation.

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 primaryShard in the command for a database and the database is already sharding enabled 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 movePrimary instead.

The following command, run from a mongos, creates the the shardTest database:

db.adminCommand( { enableSharding: "shardTest" } )
←  configureCollectionBalancingflushRouterConfig →
Share Feedback
© 2023 MongoDB, Inc.


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