{"id":44286,"date":"2015-09-23T22:31:34","date_gmt":"2015-09-23T19:31:34","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=44286"},"modified":"2023-12-06T15:07:42","modified_gmt":"2023-12-06T13:07:42","slug":"mongodb-sharding-guide","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html","title":{"rendered":"MongoDB Sharding Guide"},"content":{"rendered":"<p><em>This article is part of our Academy Course titled <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-a-scalable-nosql-db\/\">MongoDB \u2013 A Scalable NoSQL DB<\/a>.<\/em><\/p>\n<p><em>In this course, you will get introduced to MongoDB. You will learn how to install it and how to operate it via its shell. Moreover, you will learn how to programmatically access it via Java and how to leverage Map Reduce with it. Finally, more advanced concepts like sharding and replication will be explained. Check it out <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-a-scalable-nosql-db\/\">here<\/a>!<\/em><\/p>\n<div class=\"toc\">\n<h4>Table Of Contents<\/h4>\n<dl>\n<dt><a href=\"#introduction\">1. Introduction<\/a><\/dt>\n<dt><a href=\"#configuration\">2. Configuration<\/a><\/dt>\n<dt><a href=\"#sharding\">3. Sharding (Partitioning) Schemes<\/a><\/dt>\n<dd>\n<dl>\n<dt><a href=\"#range\">3.1. Range-based sharding (partitioning)<\/a><\/dt>\n<dt><a href=\"#hash\">3.2. Hash-based sharding (partitioning)<\/a><\/dt>\n<\/dl>\n<\/dd>\n<dt><a href=\"#adding\">4. Adding \/ Removing Shards<\/a><\/dt>\n<dt><a href=\"#configuring\">5. Configuring Sharded Cluster<\/a><\/dt>\n<dt><a href=\"#sharding_databases_and_collections\">6. Sharding databases and collections<\/a><\/dt>\n<dt><a href=\"#commands\">7. Sharding commands and command helpers<\/a><\/dt>\n<dt><a href=\"#next\">8. What&#8217;s next<\/a><\/dt>\n<\/dl>\n<\/div>\n<h2><a name=\"introduction\"><\/a>1. Introduction<\/h2>\n<p>Sharding is a technique used to split large amount of data across multiple server instances. Nowadays, data volumes are growing exponentially and a single physical server often is not able to store and manage such a mass. Sharding helps in solving such issues by dividing the whole data set into smaller parts and distributing them across large number of servers (or <strong>shards<\/strong>). Collectively, all <strong>shards<\/strong> make up a whole data set or, in terms of <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> , a single logical database.<\/p>\n<h2><a name=\"configuration\"><\/a>2. Configuration<\/h2>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> supports sharding out of the box using <strong>sharded clusters <\/strong>configurations: <strong>config<\/strong> <strong>servers <\/strong>(three for production deployments), one or more <strong>shards<\/strong> (replica sets for production deployments), and one or more <strong>query routing<\/strong> processes.<\/p>\n<ul>\n<li><strong>shards<\/strong> store the data: for high availability and data consistency reason, each shard should be a <a href=\"http:\/\/docs.mongodb.org\/manual\/core\/replication\/\">replica set<\/a> (we will talk more about replication in <strong>Part 5. MongoDB Replication Guide<\/strong>)<\/li>\n<li><strong>query routers<\/strong> (<strong>mongos<\/strong> processes) forward client applications and direct operations to the appropriate <strong>shard<\/strong> or <strong>shards<\/strong> (a <strong>sharded cluster<\/strong> can contain more than one <strong>query router<\/strong> to balance the load)<\/li>\n<li><strong>config servers<\/strong> store the cluster\u2019s metadata: mapping of the cluster\u2019s data set to the <strong>shards<\/strong> (the <strong>query routers<\/strong> use this metadata to target operations to specific <strong>shards<\/strong>), for high availability the production <strong>sharded cluster<\/strong> should have 3 <strong>config servers <\/strong>but for testing purposes a cluster with a single <strong>config server<\/strong> may be deployed<\/li>\n<\/ul>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shards (partitions) data on a per-collection basis. The <strong>sharded<\/strong> collections in a <strong>sharded cluster<\/strong> should be accessed through the <strong>query routers <\/strong>(<strong>mongos<\/strong> processes) only. Direct connection to a shard gives access to only a fraction of the cluster\u2019s data. Additionally, every database has a so called <strong>primary<\/strong> <strong>shard<\/strong> that holds all the unsharded collections in that database.<\/p>\n<p><strong>Config servers<\/strong> are special <strong>mongod<\/strong> process instances that store the metadata for a single <strong>sharded cluster<\/strong>. Each cluster must have its own <strong>config servers<\/strong>. <strong>Config servers<\/strong> use a two-phase commit protocol to ensure consistency and reliability. All <strong>config servers<\/strong> must be available to deploy a <strong>sharded cluster <\/strong>or to make any changes to cluster metadata. Clusters become inoperable without the cluster metadata.<\/p>\n<h2><a name=\"sharding\"><\/a>3. Sharding (Partitioning) Schemes<\/h2>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> distributes data across shards at the collection level by partitioning a collection\u2019s data by the <strong>shard key<\/strong> (for more details, please refer to <a href=\"http:\/\/docs.mongodb.org\/manual\/core\/sharding-shard-key\/\">official documentation<\/a>).<\/p>\n<p><strong>Shard key<\/strong> is either an indexed simple or compound field that exists in every document in the collection. Using either <strong>range-based partitioning<\/strong> or <strong>hash-based partitioning<\/strong>, <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> splits the shard key values into chunks and distributes those chunks evenly across the <strong>shards<\/strong>. To do that, <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> uses two background processes:<\/p>\n<ul>\n<li><strong>splitting<\/strong>: a background process that keeps chunks from growing too large. When a particular chunk grows beyond a specified chunk size (by default, <strong>64 Mb<\/strong>), <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> splits the chunk in a half. Inserts and updates of the <strong>sharded<\/strong> collection may triggers splits.<\/li>\n<li><strong>balancer<\/strong>: a background process that manages chunk migrations. The balancer runs on every query router in a cluster. When the distribution of a <strong>sharded<\/strong> collection in a cluster is uneven, the balancer moves chunks from the shard that has the largest number of chunks to the shard with the lowest number of chunks until balance is reached.<\/li>\n<\/ul>\n<h3><a name=\"range\"><\/a>3.1. Range-based sharding (partitioning)<\/h3>\n<p>In range-base sharding (partitioning), <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> splits the data set into ranges determined by the <strong>shard key<\/strong> values. For example, if <strong>shard key<\/strong> is numeric field, <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> partitions the whole field\u2019s value range into smaller, non-overlapping intervals (chunks).<\/p>\n<p>Although range-based sharding (partitioning) supports more efficient range queries, it may result into uneven data distribution.<\/p>\n<h3><a name=\"hash\"><\/a>3.2. Hash-based sharding (partitioning)<\/h3>\n<p>In hash-based sharding (partitioning), <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> computes the hash value of the <strong>shard key<\/strong> and then uses these hashes to split the data between <strong>shards<\/strong>. Hash-based partitioning ensures an even distribution of data but leads to inefficient range queries.<\/p>\n<h2><a name=\"adding\"><\/a>4. Adding \/ Removing Shards<\/h2>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> allows adding and removing <strong>shards<\/strong> to\/from running <strong>shared cluster.<\/strong> When new shards are added, it breaks the balance since the new shards have no chunks. While <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> begins migrating data to the new shards immediately, it can take a while before the cluster restores the balance.<\/p>\n<p>Accordingly, when shards are being removed from the cluster, the balancer migrates all chunks from those shards to other shards. When migration is done, the shards can be safely removed.<\/p>\n<h2><a name=\"configuring\"><\/a>5. Configuring Sharded Cluster<\/h2>\n<p>Now that we understand the <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> sharding basics, we are going to deploy a small <strong>sharded cluster<\/strong> from scratch using simplified (test-like) configuration without replica sets and single <strong>config server<\/strong> instance.<\/p>\n<p>The deployment of <strong>sharded cluster <\/strong>begins with running <strong>mongod<\/strong> server process in a <strong>config server <\/strong>mode using the <code>--configsvr<\/code> command line argument. Each <strong>config server <\/strong>requires its own data directory to store a complete the cluster\u2019s metadata and that should be provided using <code>--dbpath<\/code> command line argument (which we have already seen in <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-installation-how-to-install-mongodb\/\"><strong>Part 1. MongoDB Installation &#8211; How to install MongoDB<\/strong><\/a>). That being said, let us perform the following steps:<\/p>\n<ol>\n<li>Create a data folder (we need to do that only once): <code>mkdir configdb<\/code><\/li>\n<li>Run MongoDB server: <code>bin\/mongod --configsvr --dbpath configdb<\/code><\/li>\n<\/ol>\n<p><figure id=\"attachment_4997\" aria-describedby=\"caption-attachment-4997\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGOD.CONFIGSVR.png\"><img decoding=\"async\" class=\"size-large wp-image-4997\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGOD.CONFIGSVR-1024x300.png\" alt=\"Picture 1. Config server has been started successfully.\" width=\"1024\" height=\"300\" \/><\/a><figcaption id=\"caption-attachment-4997\" class=\"wp-caption-text\"><strong>Picture 1<\/strong>. <strong>Config server<\/strong> has been started successfully.<\/figcaption><\/figure><\/p>\n<p>By default, <strong>config servers<\/strong> listen on a port <strong>27019<\/strong>. All <strong>config servers<\/strong> should be up and running before starting up a <strong>sharded cluster<\/strong>. In our deployment, the single <strong>config server<\/strong> is run on a host with name <strong>ubuntu<\/strong>.<\/p>\n<p>Next step is to run the instances of <strong>mongos<\/strong> processes. Those are lightweight and require only knowing the location of <strong>config servers<\/strong>. For that, the command line argument <code>--configdb<\/code> is being used followed by comma-separated <strong>config server<\/strong> names (in our case, only single <strong>config server<\/strong> on host <strong>ubuntu<\/strong>):<\/p>\n<pre class=\"brush:bash\">bin\/mongos --configdb ubuntu<\/pre>\n<p><figure id=\"attachment_4998\" aria-describedby=\"caption-attachment-4998\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGOS.png\"><img decoding=\"async\" class=\"size-large wp-image-4998\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGOS-1024x342.png\" alt=\"Picture 2. Single mongos instance has been started successfully and connected to config server. \" width=\"1024\" height=\"342\" \/><\/a><figcaption id=\"caption-attachment-4998\" class=\"wp-caption-text\"><strong>Picture 2<\/strong>. Single <strong>mongos<\/strong> instance has been started successfully and connected to <strong>config server<\/strong>.<\/figcaption><\/figure><\/p>\n<p>The default port <strong>mongos <\/strong>process listens to is <strong>27017<\/strong>, the same as a usual <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server.<\/p>\n<p>The final step is to add some regular <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instances (<strong>shards<\/strong>) into the <strong>sharded cluster<\/strong>. Les us start two <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instances on different ports, <strong>27000<\/strong> and <strong>27001<\/strong> respectively.<\/p>\n<ol>\n<ol>\n<li>Run first <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instance (<strong>shard<\/strong>) on port <strong>27000<\/strong><\/li>\n<\/ol>\n<\/ol>\n<pre class=\"brush:bash\">mkdir data-shard1\nbin\/mongod --dbpath data-shard1 --port 27000\n<\/pre>\n<ol>\n<ol>\n<li>Run second <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instance (<strong>shard<\/strong>) on port <strong>27001<\/strong><\/li>\n<\/ol>\n<\/ol>\n<pre class=\"brush:bash\">mkdir data-shard2\nbin\/mongod --dbpath data-shard2 --port 27001\n<\/pre>\n<p>Once the standalone <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instances are up and running, it is a time to add them into <strong>sharded cluster<\/strong> with the help of <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell. Up to now, we have used <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell to connect to standalone <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> servers. In <strong>sharded cluster<\/strong>, the <strong>mongos <\/strong>processesare the entry points for all clients and as such we are going to connect to the one we have just run (running on default port <strong>27017<\/strong>): <code>bin\/mongo --port 27017 --host ubuntu<\/code><\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGO_.SHELL_.MONGOS.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MONGO_.SHELL_.MONGOS.png\" alt=\"\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell provides a useful command <code>sh.addShard()<\/code> for adding <strong>shards<\/strong> (please refer to <a href=\"#_Sharding_commands_and\">Shell Sharding Command Helpers<\/a> for more details). Let us issue this command against the two standalone <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instances we have run before:<\/p>\n<pre class=\"brush:bash\">sh.addShard( \"ubuntu:27000\" ) \nsh.addShard( \"ubuntu:27001\" )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ADDSHARD.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ADDSHARD.png\" alt=\"\" \/><\/a><\/p>\n<p>Let us check the current <strong>sharded cluster<\/strong> configuration by issuing another very helpful <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell command <code>sh.status()<\/code>.<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STATUS.png\"><img decoding=\"async\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STATUS.png\" alt=\"\" \/><\/a><\/p>\n<p>At this point the <strong>sharded cluster<\/strong> infrastructure is configured and we can move on with sharding database collections.<\/p>\n<h3>Shard tags<\/h3>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> allows to associate tags to specific ranges of a <strong>shard key<\/strong> with a specific <strong>shard<\/strong> or subset of <strong>shards<\/strong>. A single <strong>shard<\/strong> may have multiple tags, and multiple <strong>shards<\/strong> may also have the same tag. But any given shard key range may only have one assigned tag. The overlapping of the ranges is not allowed as well as tagging the same range more than once.<\/p>\n<p>For example, let us assign tags to each of the <strong>shards<\/strong> in our <strong>sharded cluster<\/strong> by using <code>sh.addShardTag()<\/code> command.<\/p>\n<pre class=\"brush:bash\">sh.addShardTag( \"shard0001\", \"bestsellers\" )\nsh.addShardTag( \"shard0000\", \"others\" )<\/pre>\n<p>To find all shards associated with a particular tag, the command against <strong>config<\/strong> database should be issued. For example, to find all <strong>shards<\/strong> tagged as <strong>&#8220;bestsellers&#8221; <\/strong>the following commands should be typed in <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell:<\/p>\n<pre class=\"brush:bash\">use config\ndb.shards.find( { tags: \"bestsellers\" } )<\/pre>\n<p><figure id=\"attachment_4990\" aria-describedby=\"caption-attachment-4990\" style=\"width: 1024px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.CONFIG.TAGS_.png\"><img decoding=\"async\" class=\"size-large wp-image-4990\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.CONFIG.TAGS_-1024x139.png\" alt=\"Picture 6. Finding all shards tagged as &quot;bestsellers&quot;. \" width=\"1024\" height=\"139\" \/><\/a><figcaption id=\"caption-attachment-4990\" class=\"wp-caption-text\"><strong>Picture 6<\/strong>. Finding all shards tagged as <strong>&#8220;bestsellers&#8221;<\/strong>.<\/figcaption><\/figure><\/p>\n<p>Another collection in <strong>config<\/strong> database named <strong>tags<\/strong> contains all tags definitions and may be queried the regular way for all available tags.<\/p>\n<p>Respectively, tags could be removed from the <strong>shard<\/strong> using the <code>sh.removeShardTag()<\/code>command. For example:<\/p>\n<pre class=\"brush:bash\">sh.removeShardTag( \"shard0000\", \"others\" )<\/pre>\n<h2><a name=\"sharding_databases_and_collections\"><\/a>6. Sharding databases and collections<\/h2>\n<p>As we already know, <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> performs sharding on collection level. But before a collection could be sharded, the sharding must be enabled for the collection\u2019s database. Enabling sharding for a database does not redistribute data but makes it possible to shard the collections in that database.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>For demonstration purposes, we are going to reuse the bookstore example from <a href=\"http:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-and-java-tutorial\/\"><strong>Part 3. MongoDB and Java Tutorial<\/strong><\/a> and make the <strong>books<\/strong> collection shardable. Let us reconnect to <strong>mongos<\/strong> instance by providing additionally the database name <code>bin\/mongo --port 27017 --host ubuntu bookstore<\/code>(or just issue the command <strong>use<\/strong> <strong>bookstore<\/strong>in the existing <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell session) and insert couple of documents into the <strong>books<\/strong> collection.<\/p>\n<pre class=\"brush:java\">db.books.insert( { \n    \"title\" : \"MongoDB: The Definitive Guide\", \n    \"published\" : \"2013-05-23\", \n    \"categories\" : [ \"Databases\", \"NoSQL\", \"Programming\" ], \n    \"publisher\" : { \"name\" : \"O'Reilly\" } \n} )\n\ndb.books.insert( { \n    \"title\" : \"MongoDB Applied Design Patterns\", \n    \"published\" : \"2013-03-19\", \n\t\"categories\" : [ \"Databases\", \"NoSQL\", \"Patterns\", \"Programming\" ], \n\t\"publisher\" : { \"name\" : \"O'Reilly\" } \n} )\n\ndb.books.insert( { \n    \"title\" : \"MongoDB in Action\", \n\t\"published\" : \"2011-12-16\", \n\t\"categories\" : [ \"Databases\", \"NoSQL\", \"Programming\" ], \n\t\"publisher\" : { \"name\" : \"Manning\" } \n} )\n\ndb.books.insert( { \n    \"title\" : \"NoSQL Distilled: A Brief Guide to the Emerging World of Polyglot Persistence\", \n\t\"published\" : \"2012-08-18\", \n\t\"categories\" : [ \"Databases\", \"NoSQL\" ], \n\t\"publisher\" : { \"name\" : \"Addison Wesley\" } \n} )<\/pre>\n<p>As we mentioned before, sharding is not yet enabled nor for <strong>bookstore<\/strong> database nor for <strong>books<\/strong> collection so the whole dataset ends up on <strong>primary shard <\/strong>(as we mentioned in <a href=\"#_Introduction\">Introduction<\/a> section). Let us enable sharding for <strong>bookstore<\/strong> database by issuing the command <code>sh.enableSharding()<\/code> in <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell.<\/p>\n<p><figure id=\"attachment_5002\" aria-describedby=\"caption-attachment-5002\" style=\"width: 921px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ENABLESHARDING.png\"><img decoding=\"async\" class=\"size-full wp-image-5002\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ENABLESHARDING.png\" alt=\"Picture 7. Enable sharding for bookstore database.\" width=\"921\" height=\"124\" \/><\/a><figcaption id=\"caption-attachment-5002\" class=\"wp-caption-text\"><strong>Picture 7<\/strong>. Enable sharding for <strong>bookstore<\/strong> database.<\/figcaption><\/figure><\/p>\n<p>We are getting very close to have our <strong>sharded cluster<\/strong> to actually do some work and to start sharding real collections. Each collection should have a <strong>shard key<\/strong> (please refer to <a href=\"#_Sharding_(Partitioning)_Schemes\">Sharding (Partitioning) Schemes<\/a> section) in order to be partitioned across multiple <strong>shards<\/strong>. Let us define a hash-based sharding key (please refer to <a href=\"#_Hash-based_sharding_(partitioning)\">Hash-based sharding (partitioning)<\/a> section) for <strong>books<\/strong> collection based on document <strong>_id<\/strong> field.<\/p>\n<pre class=\"brush:bash\">db.books.ensureIndex( { \"_id\": \"hashed\" } )<\/pre>\n<p>With that, let us tell <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> to shard the <strong>books<\/strong> collection using <code>sh.shardCollection()<\/code> command (please see <a href=\"#_Sharding_commands_and\">Sharding commands and command helpers<\/a> for more details).<\/p>\n<pre class=\"brush:bash\">sh.shardCollection( \"bookstore.books\", { \"_id\": \"hashed\" } )<\/pre>\n<p><figure id=\"attachment_5009\" aria-describedby=\"caption-attachment-5009\" style=\"width: 922px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.SHARDCOLLECTION.png\"><img decoding=\"async\" class=\"size-full wp-image-5009\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.SHARDCOLLECTION.png\" alt=\"Picture 8. Enable sharding for books collection.\" width=\"922\" height=\"122\" \/><\/a><figcaption id=\"caption-attachment-5009\" class=\"wp-caption-text\"><strong>Picture 8<\/strong>. Enable sharding for <strong>books<\/strong> collection.<\/figcaption><\/figure><\/p>\n<p>And with that, the <strong>sharded cluster<\/strong> is up and running! In the next section we are going to look closely on the commands available specifically to manage sharded deployments.<\/p>\n<h2><a name=\"commands\"><\/a>7. Sharding commands and command helpers<\/h2>\n<p><a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell provides a command helpers and <code>sh<\/code> context variable to simplify sharding management and deployment.<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>listShards<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Outputs a list of configured shards. It should be run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.adminCommand( { listShards: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.DB_.LISTSHARDS.png\"><img decoding=\"async\" class=\" wp-image-4993\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.DB_.LISTSHARDS.png\" alt=\"\" width=\"709\" height=\"312\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/listShards\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/listShards\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">listShards<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>shardConnPoolStats<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Reports statistics on the pooled and cached connections in the sharded connection pool.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.runCommand( { shardConnPoolStats: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SHARDCONNPOOLSTATS.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5013\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SHARDCONNPOOLSTATS.png\" alt=\"04.SHARDCONNPOOLSTATS\" width=\"706\" height=\"622\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardConnPoolStats\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardConnPoolStats\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">shardConnPoolStats<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>connPoolStats<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Reports statistics regarding the number of open connections to the current database instance, including client connections and server-to-server connections for replication and clustering.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.runCommand( { connPoolStats: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.CONNPOOLSTATS.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4991\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.CONNPOOLSTATS.png\" alt=\"04.CONNPOOLSTATS\" width=\"670\" height=\"594\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/connPoolStats\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/connPoolStats\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">connPoolStats<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>isdbgrid<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">This command verifies that a process <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell is connected to is a <strong>mongos<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.runCommand( { isdbgrid: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.ISDBGRID.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4995\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.ISDBGRID.png\" alt=\"04.ISDBGRID\" width=\"637\" height=\"96\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/isdbgrid\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/isdbgrid\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">isdbgrid<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>flushRouterConfig<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Clears the current <strong>sharded cluster<\/strong> information cached by a <strong>mongos<\/strong> instance and reloads all <strong>sharded cluster<\/strong> metadata from the config database. It should be issued against <strong>mongos<\/strong> process instance and run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.adminCommand( { flushRouterConfig: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.FLUSHROUTERCONFIG.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4994\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.FLUSHROUTERCONFIG.png\" alt=\"04.FLUSHROUTERCONFIG\" width=\"722\" height=\"108\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/flushRouterConfig\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/flushRouterConfig\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">flushRouterConfig<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>shardingState<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Reports if the current instance of <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server is a member of a <strong>sharded cluster<\/strong>. It should be run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">Let us connect to any of the standalone <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> server instances using <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell:<\/p>\n<pre class=\"brush:bash\">bin\/mongo --port 27001<\/pre>\n<p>And issue the command:<\/p>\n<pre class=\"brush:bash\">db.adminCommand( { shardingState: 1 } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SHARDINGSTATE.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5014\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SHARDINGSTATE.png\" alt=\"04.SHARDINGSTATE\" width=\"584\" height=\"233\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardingState\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardingState\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">shardingState<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>movePrimary<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    movePrimary : &lt;database&gt;, \n    to : &lt;shard name&gt; \n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command reassigns the database\u2019s primary shard, which holds all unsharded collections in the database, to another shard in the <strong>sharded cluster<\/strong>. It should be issued against <strong>mongos<\/strong> process instance and run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.adminCommand\n( { movePrimary : \"test\", to : \"shard0001\" } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MOVEPRIMARY.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4999\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.MOVEPRIMARY.png\" alt=\"04.MOVEPRIMARY\" width=\"655\" height=\"98\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/movePrimary\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/movePrimary\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">movePrimary<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.help()<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Outputs the brief description for all sharding-related shell functions.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.help()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.HELP_.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5005\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.HELP_-1024x355.png\" alt=\"04.SH.HELP\" width=\"753\" height=\"261\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.help\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.help\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.help()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>addShard<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    addShard: &lt;hostname&gt;&lt;:port&gt;, \n    maxSize: &lt;size&gt;, \n    name: &lt;shard_name&gt; \n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Wrapper<\/strong><\/td>\n<td width=\"576\"><strong>sh.addShard(&lt;host&gt;)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command adds either a database instance or a replica set to a <strong>sharded cluster<\/strong>. It should be issues against <strong>mongos<\/strong> process instance.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Configuring_Sharded_Cluster\">Configuring Sharded Cluster<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/addShard\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/addShard\/<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addShard\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addShard\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">addShard<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.printShardingStatus\/#db.printShardingStatus\">db.printShardingStatus()<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.printShardingStatus\/#db.printShardingStatus\">sh.status()<\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command outputs a formatted report of the sharding configuration and the information regarding existing chunks in a <strong>sharded cluster<\/strong>. The default behavior suppresses the detailed chunk information if the total number of chunks is greater than or equal to 20.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Configuring_Sharded_Cluster\">Configuring Sharded Cluster<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.status\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.status\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">db.printShardingStatus()<\/p>\n<p>sh.status()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>enableSharding<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    enableSharding: &lt;database&gt; \n}\n<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Wrapper<\/strong><\/td>\n<td width=\"576\"><strong>sh.enableSharding(database)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Enables sharding on the specified database. It does not automatically shard any collections but makes it possible to begin sharding collections using <strong>sh.shardCollection()<\/strong> command.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Sharding_databases_and\">Sharding databases and collections<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/enableSharding\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/enableSharding\/<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.enableSharding\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.enableSharding\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">enableSharding<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>shardCollection<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    shardCollection: &lt;database&gt;.&lt;collection&gt;, \n    key: &lt;shardkey&gt; \n}<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Wrapper<\/strong><\/td>\n<td width=\"576\"><strong>sh.shardCollection(namespace, key, unique)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command enables sharding for a collection and allows <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> to begin distributing data among <strong>shards<\/strong>. Before issuing this command, the sharding should be enabled on database level using <strong>sh.enableSharding()<\/strong> command.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Sharding_databases_and\">Sharding databases and collections<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardCollection\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/shardCollection\/<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.shardCollection\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.shardCollection\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">shardCollection<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.getBalancerHost()<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command returns the name of a <a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/program\/mongos\/#bin.mongos\"><strong>mongos<\/strong><\/a> responsible for the balancer process.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.getBalancerHost()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.GETBALANCERHOST.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5003\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.GETBALANCERHOST.png\" alt=\"04.SH.GETBALANCERHOST\" width=\"783\" height=\"117\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.getBalancerHost\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.getBalancerHost\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.getBalancerHost()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.getBalancerState()<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command returns <strong>true <\/strong>when the balancer is enabled and <strong>false<\/strong> if the balancer is disabled.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.getBalancerState()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.GETBALANCERSTATE.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5004\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.GETBALANCERSTATE.png\" alt=\"04.SH.GETBALANCERSTATE\" width=\"763\" height=\"115\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.getBalancerState\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.getBalancerState\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.getBalancerState()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.isBalancerRunning()<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command returns <strong>true <\/strong>if the balancer process is currently running and migrating chunks and <strong>false<\/strong> if the balancer process is not running.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.isBalancerRunning()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ISBALANCERRUNNING.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5006\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.ISBALANCERRUNNING.png\" alt=\"04.SH.ISBALANCERRUNNING\" width=\"733\" height=\"106\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.isBalancerRunning\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.isBalancerRunning\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.isBalancerRunning()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>setBalancerState (true | false)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command enables or disables the balancer in the <strong>sharded cluster<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.setBalancerState(false)<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.SETBALANCERSTATE.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5008\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.SETBALANCERSTATE.png\" alt=\"04.SH.SETBALANCERSTATE\" width=\"765\" height=\"134\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.disableBalancing\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.disableBalancing\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">setBalancerState (true | false)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.startBalancer(timeout, interval)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command enables the balancer in a <strong>sharded cluster<\/strong> and waits for balancing to start.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.startBalancer(5000, 100)<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STARTBALANCER.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5010\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STARTBALANCER.png\" alt=\"04.SH.STARTBALANCER\" width=\"709\" height=\"86\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.startBalancer\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.startBalancer\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.startBalancer(timeout, interval)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.stopBalancer(timeout, interval)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command disables the balancer in a <strong>sharded cluster<\/strong> and waits for balancing to finish.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">sh.stopBalancer(5000, 100)<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STOPBALANCER.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5012\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.STOPBALANCER.png\" alt=\"04.SH.STOPBALANCER\" width=\"745\" height=\"147\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.stopBalancer\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.stopBalancer\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.stopBalancer(timeout, interval)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>db.&lt;collection&gt;.getShardDistribution()<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">Outputs the data distribution statistics for a sharded collection in the <strong>sharded cluster<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the commands:<\/p>\n<pre class=\"brush:bash\">use bookstore<\/pre>\n<pre class=\"brush:bash\">db.books.getShardDistribution()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.DB_.COLLECTION.GETSHARDDISTRIBUTION.png\"><img decoding=\"async\" class=\"aligncenter wp-image-4992\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.DB_.COLLECTION.GETSHARDDISTRIBUTION.png\" alt=\"04.DB.COLLECTION.GETSHARDDISTRIBUTION\" width=\"789\" height=\"323\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.collection.getShardDistribution\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/db.collection.getShardDistribution\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">db..getShardDistribution()<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>removeShard<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{\n    removeShard: &lt;shard name&gt;\n}<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command removes a shard from a <strong>sharded cluster<\/strong>. Upon run, <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> moves the shard\u2019s chunks to other <strong>shards<\/strong> in the cluster and only than removes the <strong>shard<\/strong>. It should be run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<pre class=\"brush:bash\">db.adminCommand( { removeShard: \"shard0002\" } )<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.REMOVESHARD.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5000\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.REMOVESHARD.png\" alt=\"04.REMOVESHARD\" width=\"741\" height=\"199\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/removeShard\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/removeShard\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">removeShard<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>mergeChunks<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    mergeChunks : &lt;database&gt;.&lt;collection&gt; ,\n    bounds : [ \n        { &lt;shardKeyField&gt;: &lt;minFieldValue&gt; },\n        { &lt;shardKeyField&gt;: &lt;maxFieldValue&gt; } \n    ]\n}<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command combines two contiguous chunk ranges the same shard into a single chunk. At least one of chunk must not have any documents. The command should be issued against <strong>mongos<\/strong> instance and run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/mergeChunks\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/mergeChunks\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">mergeChunks<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>split<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Parameters<\/strong><\/td>\n<td width=\"576\">\n<pre class=\"brush:java\">{ \n    split: &lt;database&gt;.&lt;collection&gt;,\n    &lt;find|middle|bounds&gt; \n}<\/pre>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Wrapper<\/strong><\/td>\n<td width=\"576\"><strong>sh.splitAt(namespace, query)<\/strong><\/p>\n<p><strong>sh.splitFind(namespace, query)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command manually splits a chunk in a <strong>sharded cluster<\/strong> into two chunks. The command should be run in context of <strong>admin<\/strong> database.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the command:<\/p>\n<p>db.adminCommand( { split: &#8220;bookstore.books&#8221;, find: { &#8220;title&#8221;: &#8220;MongoDB in action&#8221; } } )<\/p>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SPLIT_.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5015\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SPLIT_.png\" alt=\"04.SPLIT\" width=\"690\" height=\"89\" \/><\/a><\/p>\n<p>Alternatively, let us run the same command using command wrapper:<\/p>\n<p>sh.splitFind( &#8220;bookstore.books&#8221;, { &#8220;title&#8221;: &#8220;MongoDB in action&#8221; } )<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/command\/split\/\">http:\/\/docs.mongodb.org\/manual\/reference\/command\/split\/<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.splitAt\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.splitAt\/<\/a><\/p>\n<p><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.splitFind\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.splitFind\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">split<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"84\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.moveChunk(namespace, query, shard name)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command moves the chunk that contains the document specified by the query to another shard with name <strong>shard name<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Example<\/strong><\/td>\n<td width=\"576\">In <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> shell, let us issue the commands:<\/p>\n<pre class=\"brush:bash\">use bookstore<\/pre>\n<pre class=\"brush:bash\">sh.moveChunk(   \"bookstore.books\", \n{ \"title\": \"MongoDB in action\" }, \"shard0001\" )<\/pre>\n<pre class=\"brush:bash\">db.books.getShardDistribution()<\/pre>\n<p><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.MOVECHUNKS.png\"><img decoding=\"async\" class=\"aligncenter wp-image-5007\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2015\/09\/04.SH_.MOVECHUNKS.png\" alt=\"04.SH.MOVECHUNKS\" width=\"765\" height=\"455\" \/><\/a><\/td>\n<\/tr>\n<tr>\n<td width=\"84\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.moveChunk\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.moveChunk\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.moveChunk(namespace, query, shard name)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.addShardTag(shard, tag)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command associates a <strong>shard<\/strong> with a tag or identifier. <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> uses these identifiers to direct chunks that fall within a tagged range to specific <strong>shards<\/strong>.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Shard_tags\">Shard tags<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addShardTag\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addShardTag\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.addShardTag(shard, tag)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.addTagRange(namespace, minimum, maximum, tag)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command attaches a range of shard key values to a shard tag created previously using the <strong>sh.addShardTag()<\/strong> command.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Shard_tags\">Shard tags<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addTagRange\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.addTagRange\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.addTagRange(namespace, minimum, maximum, tag)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"wp-caption aligncenter\">\n<table>\n<tbody>\n<tr>\n<td width=\"85\"><strong>Command<\/strong><\/td>\n<td width=\"576\"><strong>sh.removeShardTag(shard, tag)<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Description<\/strong><\/td>\n<td width=\"576\">The command removes the association between a tag and a shard. The command should be issued against <strong>mongos<\/strong> instance<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Example<\/strong><\/td>\n<td width=\"576\">See please <a href=\"#_Shard_tags\">Shard tags<\/a> section.<\/td>\n<\/tr>\n<tr>\n<td width=\"85\"><strong>Reference<\/strong><\/td>\n<td width=\"576\"><a href=\"http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.removeShardTag\/\">http:\/\/docs.mongodb.org\/manual\/reference\/method\/sh.removeShardTag\/<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"wp-caption-text\">sh.removeShardTag(shard, tag)<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<h2><a name=\"next\"><\/a>8. What&#8217;s next<\/h2>\n<p>In this part we have covered basics of <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> sharding (partitioning) capabilities. For more complete and comprehensive details please refer to <a href=\"http:\/\/docs.mongodb.org\/manual\/sharding\/\">official documentation<\/a>. In the next section we are going to take a look on <a href=\"http:\/\/www.mongodb.org\/\">MongoDB<\/a> replication.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to install it and how to operate it via its shell. Moreover, you will learn how to programmatically access it via Java and how to leverage Map &hellip;<\/p>\n","protected":false},"author":141,"featured_media":187,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[112],"class_list":["post-44286","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-software-development","tag-mongodb"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>MongoDB Sharding Guide - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MongoDB Sharding Guide - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2015-09-23T19:31:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-06T13:07:42+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Andrey Redko\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Andrey Redko\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"18 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html\"},\"author\":{\"name\":\"Andrey Redko\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\"},\"headline\":\"MongoDB Sharding Guide\",\"datePublished\":\"2015-09-23T19:31:34+00:00\",\"dateModified\":\"2023-12-06T13:07:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html\"},\"wordCount\":3125,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"keywords\":[\"MongoDB\"],\"articleSection\":[\"Software Development\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html\",\"name\":\"MongoDB Sharding Guide - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"datePublished\":\"2015-09-23T19:31:34+00:00\",\"dateModified\":\"2023-12-06T13:07:42+00:00\",\"description\":\"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/mongodb-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2015\\\/09\\\/mongodb-sharding-guide.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Software Development\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/software-development\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MongoDB Sharding Guide\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/771a6504862edc45322776832cbce413\",\"name\":\"Andrey Redko\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g\",\"caption\":\"Andrey Redko\"},\"description\":\"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\\\/EE, C#\\\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).\",\"sameAs\":[\"http:\\\/\\\/aredko.blogspot.com\\\/\",\"http:\\\/\\\/ca.linkedin.com\\\/in\\\/aredko\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/andrey-redko\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MongoDB Sharding Guide - Java Code Geeks","description":"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html","og_locale":"en_US","og_type":"article","og_title":"MongoDB Sharding Guide - Java Code Geeks","og_description":"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to","og_url":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2015-09-23T19:31:34+00:00","article_modified_time":"2023-12-06T13:07:42+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","type":"image\/jpeg"}],"author":"Andrey Redko","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Andrey Redko","Est. reading time":"18 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html"},"author":{"name":"Andrey Redko","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413"},"headline":"MongoDB Sharding Guide","datePublished":"2015-09-23T19:31:34+00:00","dateModified":"2023-12-06T13:07:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html"},"wordCount":3125,"commentCount":0,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","keywords":["MongoDB"],"articleSection":["Software Development"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html","url":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html","name":"MongoDB Sharding Guide - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","datePublished":"2015-09-23T19:31:34+00:00","dateModified":"2023-12-06T13:07:42+00:00","description":"This article is part of our Academy Course titled MongoDB \u2013 A Scalable NoSQL DB. In this course, you will get introduced to MongoDB. You will learn how to","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/mongodb-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2015\/09\/mongodb-sharding-guide.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Software Development","item":"https:\/\/www.javacodegeeks.com\/category\/software-development"},{"@type":"ListItem","position":3,"name":"MongoDB Sharding Guide"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/771a6504862edc45322776832cbce413","name":"Andrey Redko","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/16419ce8394173028eddaeb992859862bab50cfcf74589fa9bb9a3dd8bb27518?s=96&d=mm&r=g","caption":"Andrey Redko"},"description":"Andriy is a well-grounded software developer with more then 12 years of practical experience using Java\/EE, C#\/.NET, C++, Groovy, Ruby, functional programming (Scala), databases (MySQL, PostgreSQL, Oracle) and NoSQL solutions (MongoDB, Redis).","sameAs":["http:\/\/aredko.blogspot.com\/","http:\/\/ca.linkedin.com\/in\/aredko"],"url":"https:\/\/www.javacodegeeks.com\/author\/andrey-redko"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/141"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=44286"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/44286\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/187"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=44286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=44286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=44286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}