Defining a sharding schema with a hash algorithm
The shardCollection command in the MongoDB shell creates a definition for distributing data across the database servers of a shard cluster.
Procedure
To create a shard-cluster definition that uses a hash
algorithm for distributing data across database servers:
- Run the mongo command.The command starts the MongoDB shell.
- Run the shardCollection command.
There are two ways to run the command:
- Run the sh.shardCollection MongoDB command.
For example:
> sh.shardCollection("database1.collection1", {customer_name:"hashed"})
- Run the db.runCommand from the MongoDB shell,
with shardCollection command syntax. For example:
> db.runCommand({"shardCollection":"database2.collection_2", key:{customer_name:"hashed"}})
The shardCollection command syntax for using a hash algorithm is shown in the following diagram:db.runCommand ({"shardCollection":" database. { collection | table } ", key:{ { field | column } :"hashed"}})Element Description Restrictions database The name of the database that contains the collection that is distributed across database servers. The database must exist. collection The name of the collection that is distributed across database servers. The collection must exist. column The shard key that is used to distribute data across the database servers of a shard cluster. The column must exist. Composite shard keys are not supported.
field The shard key that is used to distribute data across the database servers of a shard cluster. The field must exist. Composite shard keys are not supported.
table The name of the table that is distributed across database servers. The table must exist.
- Run the sh.shardCollection MongoDB command.
For example:
- For optimal query performance, connect to the wire listener and run the MongoDB ensureIndex command on the shard key of each of a cluster's shard servers. The ensureIndex command ensures that an index for the collection or table is created on the shard server.
Results
sh_database_
{
collection
| table
}
Example
The following command defines a
shard cluster that uses a hash algorithm on the shard key value year to
distribute data across multiple database servers.
> sh.shardCollection("mydatabase.mytable",{year:"hashed"})
The name of the created shard-cluster definition is sh_mydatabase_mytable.