shardCollection

shardCollection

The shardCollection command marks a collection for sharding and will allow data to begin distributing among shards. You must run enableSharding on a database before running the shardCollection command.

{ shardCollection: "<database>.<collection>", key: <shardkey> }

This enables sharding for the collection specified by <collection> in the database named <database>, using the key <shardkey> to distribute documents among the shard. <shardkey> is a document and takes the same form as an index specification document.

Parameters:
  • shardCollection (string) – Specify the namespace of a collection to shard in the form <database>.<collection>.
  • key (document) –

    Specify the index specification to use as the shard key. The index must exist prior to the shardCollection command unless the collection is empty. If the collection is empty, then MongoDB will create the index prior to sharding the collection.

    New in version 2.4: The key may be in the form { field : "hashed" } which will use the specified field as a hashed shard key .

  • unique (boolean) – When true, the unique option ensures that the underlying index enforces a unique constraint. Hashed shard keys do not support unique constraints.
  • numInitialChunks (integer) –

    New in version 2.4.

    Specify the number of chunks to create upon sharding the collection. The collection will then be pre-split and balanced across the specified number of chunks.

    You can specify no more than 8192 chunks using numInitialChunks.

Choosing the right shard key to effectively distribute load among your shards requires some planning. Also review Shard Keys regarding choosing a shard key.

Warning

MongoDB provides no method to deactivate sharding for a collection after calling shardCollection. Additionally, after shardCollection, you cannot change shard keys or modify the value of any field used in your shard key index.

MongoDB Manual 2.4

Formats

About MongoDB

MongoDB Ecosystem

MongoDB Resources