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:
  1. Run the mongo command.
    The command starts the MongoDB shell.
  2. 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:
      
      1 db.runCommand ({"shardCollection":"database.
      2.1 collection
      2.1 table
      1", key:{
      2.1 field
      2.1 column
      1:"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.
  3. 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

The name of a shard-cluster definition that is created by a shardCollection command that is run through the wire listener is:

1 sh_database_
2.1 collection
2.1 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.