- Reference >
mongosh
Methods >- Sharding Methods >
- sh.enableSharding()
sh.enableSharding()¶
On this page
Definition¶
-
sh.
enableSharding
(database, primaryShard)¶ Note
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
methodsh.shardCollection()
to shard collections on the database. Themongosh
methodsh.enableSharding()
wraps theenableSharding
command.
Syntax¶
The sh.enableSharding
has the following form:
Parameter¶
The sh.enableSharding()
method takes the following parameter:
Parameter | Type | Description |
---|---|---|
database | String | The name of the database you want to create. |
primaryShard | String | 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. Warning Tip 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) |
Behavior¶
Write Concern¶
mongos
uses
"majority"
for the
enableSharding
command and its helper
sh.enableSharding()
.
Primary Shard¶
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.
Example¶
The following example, run from a mongos
, explicitly
creates the shardTest
database:
To verify, run sh.status()
.