Defining a sharding schema with an expression
The MongoDB shell db.runCommand
command
with shardCollection
command syntax creates a definition
for distributing data across the database servers of a shard cluster.
Procedure
To create a shard-cluster definition that uses an expression
for distributing data across database servers:
Results
Examples
- Define a shard cluster that uses an expression to distribute data across multiple database servers
- The following command defines a shard cluster that uses an expression
on the field value state for distributing collection1 across
multiple database servers.
> db.runCommand({"shardCollection":"database1.collection1", key:{state:1},expressions:{"g_shard_server_1":"in ('KS','MO')", "g_shard_server_2":"in ('CA','WA')","g_shard_server_3":"remainder"}})
The name of the created shard-cluster definition is sh_database1_collection1.- Inserted documents with KS and MO values in the state field are sent to g_shard_server_1.
- Inserted documents with CA and WA values in the state field are sent to g_shard_server_2.
- All inserted documents that do not have KS, MO, CA, or WA values in the state field are sent to g_shard_server_3.
- Define a shard cluster that uses an expression to distribute data across multiple database servers
- The following command defines a shard cluster that uses an expression
on the column value animal for distributing table2 across
multiple database servers.
> db.runCommand({"shardCollection":"database1.table2", key:{animal:1},expressions:{"g_shard_server_1":"in ('dog','coyote')", "g_shard_server_2":"in ('cat')","g_shard_server_3":"in ('rat')", "g_shard_server_4":"remainder"}})
The name of the created shard-cluster definition is sh_database2_table2.- Inserted rows with dog or coyote values in the animal column are sent to g_shard_server_1.
- Inserted rows with cat values in the animal column are sent to g_shard_server_2.
- Inserted rows with rat data values in the animal column are sent to g_shard_server_3.
- All inserted rows that do not have dog, coyote, cat, or rat values in the animal column are sent to g_shard_server_4.
- Define a shard cluster that uses an expression to distribute collections across multiple database servers
- The following command defines a shard cluster that uses an expression
on the field value year for distributing collection3 across
multiple database servers.
> db.runCommand({"shardCollection":"database1.collection3", key:{year:1},expressions:{"g_shard_server_1":"between 1980 and 1989", "g_shard_server_2":"between 1990 and 1999", "g_shard_server_3":"between 2000 and 2009", "g_shard_server_4":"remainder"}})
The name of the created shard-cluster definition is sh_database3_collection3.- Inserted documents with values of 1980 to 1989 in the year field are sent to g_shard_server_1.
- Inserted documents with values of 1990 to 1999 in the year field are sent to g_shard_server_2.
- Inserted documents with values of 1980 to 1989 in the year field are sent to g_shard_server_3.
- Inserted documents with values below 1980 or above 2009 in the year field are sent to g_shard_server_4.