Shard a Collection¶
On this page
To shard a collection, you need to enable sharding.
sh.shardCollection(<namespace>, <key>) // Optional parameters omitted
For more information on the sharding method, see
Shard Key Fields and Values¶
Missing Shard Key Fields¶
Starting in version 4.4, documents in sharded collections can be
missing the shard key fields. A missing shard key falls into the
same range as a
null-valued shard key. See Missing Shard Key Fields.
In version 4.2 and earlier, shard key fields must exist in every document to be able to shard a sharded collection. To set missing shard key fields, see Set Missing Shard Key Fields.
Change a Document's Shard Key Value¶
Starting in MongoDB 4.2, you can update a document's shard key value
unless the shard key field is the immutable
_id field. In
MongoDB 4.2 and earlier, a document's shard key field value is
For details on updating the shard key value, see Change a Document's Shard Key Value.
Change a Collection's Shard Key¶
Starting in MongoDB 5.0, you can reshard a collection by changing a document's shard key.
Starting in MongoDB 4.4, you can refine a shard key by adding a suffix field or fields to the existing shard key.
In MongoDB 4.2 and earlier, the choice of shard key cannot be changed after sharding.