MongoDB Sharding Commands

Sharding is a method to distribute the data across different machines. Sharding can be used by MongoDB to support deployment on very huge scale data sets and high throughput operations.

MongoDB Sharding Commands

MongoDB sh.addShard(<url>) command

A shard replica set added to a sharded cluster using this command. If we add it among the shard of cluster, it affects the balance of chunks. It starts transferring chunks to balance the cluster.

<replica_set>/<hostname><:port>,<hostname><:port>, ...

Syntax:

Example:

Output:

MongoDB Sharding Commands

It will add a shard to specify the name of the replica set and the hostname of at least one member of the replica set.

MongoDB sh.addShardTag() command

This command is backed with a tag or identifier in a shard. MongoDB uses these identifiers to send the thread that fall within a tagged range to specified shards.

Example:

It will add three tags to three different shards.

MongoDB Sharding Commands

MongoDB sh.addShardToZone(shard, zone)

This command adds the specified shard to the specified zone. This command is introduced in version 3.4. Only those chunks are assigned to the shards that are covered by the zone.

Example:

// associates LGA to shard0000:

// shard0000 associates with both the LGA zone and the JFK zone.

MongoDB sh.addTagRange(namespace, min, max, tag) command

A range of shard key value will be attached to a shard tag using this command. The zone ranges always exclude the upper bound and include the lower bound.

Example:

We have a shard key of {state: 1, zip: 1},

The example below generates a specific tag range covering zip codes in California state:

MongoDB sh.disableBalancing(name_space) command

The balancer for the specified shard in the argument will be disabled using this command. It doesn't have any effect on the balancing of the chunks but for the other sharded collection in the same cluster.

MongoDB sh.enableBalancing(namespace) command

The command enables the balancer for the specified shard in the argument. It doesn't have any effect on the balancing of the chunks but for the other sharded collection in the same cluster.

MongoDB sh.disableAutoSplit command

This command removes the autosplit flag in the configuration setting collection. When sharded cluster have the auto-splitting option enabled, it automatically splitted in chunks by MongoDB based on the shard key values.

sh.disableAutoSplit() can be executed only from a mongo shell connected to the specified mongos instance. sh.disableAutoSplit() command returns errors if run on a mongod instance

MongoDB updateZoneKeyRange command

The relationship between a range of shard key values and a zone will be created or removed with the help of this command. Use the db.runCommand( { <command> } ) method to run the updateZoneKeyRange. You must run addShardToZone on the admin database.

Syntax:

Example:

The following example creates a range with a lower bound of 1 and an upper bound of 10 on the alpha zone:

The following query removes the previously created range by passing null to the zone field.

Now check the status of your shard:

MongoDB Sharding Commands




Latest Courses