When partitioning a collection, the shard key is the core mechanism MongoDB uses to distribute a collection across shards, ensuring data is organized efficiently.
The purpose of a shard key is to evenly distribute read and write operations across all shards—at scale. Additionally, read operations should target the lowest number of shards possible. Achieving this goal reduces latency for important queries.
To do this, be sure to carefully analyze critical query patterns and potential shard key candidates.
To achieve an optimal shard key, let’s look at the different parts of a shard key and how they affect the sharding of a collection.