Javatpoint Logo
Javatpoint Logo

MongoDB Sharding Methods

#1. sh.addShard(<url>)

In any sharded cluster, we can use this method to add a shard replica set to. The add shard method always runs on a mongos instance.

Example

The replica set and the hostname must be specified. If you specify additional hostnames for any one member of the replica set, all the hostname must be related to the same replica set if we specify any additional hostname.

The example below adds a replica set named replJTP and also specifies one member of the replica set:

#2. sh.addShardTag(shard, tag)

This method is used to return to sh.addShardToZone() and assign a shard id to each shard. The specified functionality still applies to MongoDB 3.2. But in MongoDB 3.4, it provides Zone sharding as the child of tag-aware sharding.

Example

The following example adds three tags, Java, MongoDB, and Python, to three shards:

#3. sh.enableSharding(database, primaryShard)

As the name of the method suggests, it is used to enable sharding for the specified database in the argument. When we enable sharding for any database, we can use sh.shardCollection() method to shard the specified database's collection. This method wraps the enableSharding command.

Syntax

Example

The following example enables sharding for the JTP database that runs from a mongos:

#4. sh.startBalancer(timeout, interval)

This method is used for a sharded cluster to start the balancer for the specified time and interval. The auto-splitting option can be enabled for the sharded cluster using this method. This method is also a wrapper around the command balencerStart. i.e. to start the balancing the method does not wait.

#5. sh.stopBalancer(timeout, interval)

The stop balance method is used to disables the running balancer in a sharded cluster. The auto-splitting for the specified sharded cluster can be removed using this method. sh.stopBalancer() method returns error if run on mongod instance and we can run it only on a mongos instance.

#6. convertShardKeyToHashed(<Object>)

The method returns the hashed value for the input object that is specified in the argument. The method converts a specified shard key to hashed method that uses the similar hashing function that is used by the hashed index. Also, for the specific key in the argument, the method can be used to see what the hashed value would be.

Example

Consider a sharded collection that uses a hashed shard key [1]

The hashed value of the _id field is used to distribute the document if the following document exists in the collection:

#7. sh.updateZoneKeyRange(namespace, minimum, maximum, zone)

Associates a range of shard key values with a zone. The method's helpers sh.updateZoneKeyRange() and sh.addTagRange() can run this method on database command and on collection that is unsharded. The range of shard key-values that had a lower and upper bound overlap cannot be created with the help of an existing range of the sharded collection.

Let's take an example that we have an existing range of 10 to 20 key-value. We cannot create a new range of 15 to 30, as the new range would overlap with the existing range.

Example

We have a shard collection in the example query below i.e. javaTpoint.collection with a shard key of { t : 10 }. It creates a range with a lower bound of 10 and an upper bound of 20 located at the alpha zone:

Now, by passing null to the zone field the operation below removes the previously created range.

The bounds of the target range must match the min and max value. The query below attempts to remove the old range, but specifies { t : 0 } as the minimum bound:


Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA