Choosing a shard key

The shard key (https://docs.mongodb.com/manual/sharding/#shard-keys) is a document field (or fields) within the collection which forms the basis upon which MongoDB will distribute documents between the shards. This field (or fields) must be immutable (it cannot be changed once the document is saved), and must exist in every single document in the collection to be sharded. A collection can only have one shard key, and once chosen, cannot be changed. If no index for the shard key field exists, one is created automatically. The shard key is then used to determine how the chunks are created, which are then distributed across the shards.

Shard keys are chosen based on the following criteria:

Get MongoDB 4 Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.