Managing shards

You can enable sharding for clusters that have MongoDB version 4.0 or higher. You can also add and configure individual shards.

Warning

Once sharding is enabled, the only right way to access the database is over the mongos hosts that store and update shard data. Don't forget to change the host addresses in your app code.

Make sure that your shards consist of at least 3 hosts to ensure higher availability. We don't recommend sharding small collections: query processing is faster with a standard replica cluster.

Enable sharding

Using the Managed Service for MongoDB interface, you can quickly create a MongoDB sharding infrastructure: the mongos and mongocfg hosts needed to run the sharded database.

To learn how to directly shard your MongoDB database and collections, see Sharding collections MongoDB.

  1. Go to the folder page and select Managed Service for MongoDB.

  2. Click the name of a cluster and open the Shards tab.

  3. Click Enable.

  4. Configure the mongocfg and mongos hosts that should provide access to the sharded data.

    To ensure continuous access to your data, Managed Service for MongoDB lets you create at least 3 mongocfg hosts and 2 mongos hosts.

  5. Click Enable sharding.

The cluster update starts. This operation creates the requested mongocfg and mongos hosts as well as the first shard of the cluster.

You can enable sharding for the cluster using the enableSharding method.

List shards in a cluster

  1. Go to the folder page and select Managed Service for MongoDB.

  2. Click the name of a cluster and open the Shards tab.

To list the shards in a cluster, use the listShards method.

Add a shard

The number of shards in Managed Service for MongoDB clusters is limited by the CPU and RAM quotas available to DB clusters in your cloud. To check the resources in use, open the Quotas page and find the Yandex Managed Service for MongoDB block.

  1. Go to the folder page and select Managed Service for MongoDB.
  2. Click on the name of the cluster you need and go to the Hosts tab.
  3. Click Add shard.
  4. Enter a name for the shard and add the desired number of hosts.
  5. Click Create shard.

To add a host to the cluster, use the addHosts method.

Delete a shard

You can delete a shard from a MongoDB cluster provided that it's not the only shard in it. To replace the only shard in a cluster, first create a new shard and then remove the old one.

  1. Go to the folder page and select Managed Service for MongoDB.

  2. Click on the name of a cluster and open the Shards tab.

  3. Click image in the line of the desired host and select Delete.

Use the deleteHosts method to delete a host.