Changing the definition for a shard cluster
The db.runCommand command with changeShardCollection command
syntax changes the definition for a shard cluster.
Before you begin
If the shard cluster uses an expression for distributing
data across multiple database servers, you must add database servers
to a shard cluster and remove database servers from a shard cluster
by running the changeShardCollection command. If
the shard-cluster definition uses a hash algorithm, database servers
are automatically added to the shard cluster when you run the sh.addShard MongoDB shell
command.
If you change a shard-cluster definition to include
a new shard server, that server must first be added to a shard cluster
by running the db.runCommand command with addShard command
syntax.
When a shard-cluster definition changes, existing data on shard servers is redistributed to match the new definition.
About this task
The following steps apply to changing the definition for shard cluster that uses an expression for distributing documents in a collection across multiple database servers.
Procedure
Example
You have a shard cluster that is composed of three database servers, and the shard cluster is defined by the following command:> db.runCommand({"shardCollection":"database1.collection1",
expressions:{"g_shard_server_1":"in ('KS','MO')",
"g_shard_server_2":"in ('CA','WA')","g_shard_server_3":"remainder"})To
add g_shard_server_4 and g_shard_server_5 to the shard
cluster and change where data is sent to, run the following command:> db.runCommand({"changeShardCollection":"database1.collection1",
expressions:{"g_shard_server_1":"in ('KS','MO')",
"g_shard_server_2":"in ('TX','OK')","g_shard_server_3":"in ('CA','WA')",
"g_shard_server_4":"in ('OR','ID')","g_shard_server_5":"remainder"})The
new shard cluster contains five database servers:- Inserted documents with a state field value of
KSorMOare sent to g_shard_server_1. - Inserted documents with a state field value of
TXorOKare sent to g_shard_server_2. - Inserted documents with a state field value of
CAorWAare sent to g_shard_server_3. - Inserted documents with a state field value of
ORorIDare sent to g_shard_server_4. - Inserted documents with a state field value that is not in the expression are sent to g_shard_server_5.
> db.runCommand({"changeShardCollection":"database1.collection1",
expressions:{"g_shard_server_1":"in ('KS','MO')",
"g_shard_server_3":"in ('TX','CA','WA')",
"g_shard_server_4":"in ('OK','OR','ID')",
"g_shard_server_5":"remainder"})The new shard cluster
contains four database servers. - Inserted documents with a state field value of
TXare now sent to g_shard_server_3. - Inserted documents with a state field value of
OKare now sent to g_shard_server_4.