Problem Statement
Which characteristic is MOST important when choosing a shard key?
Explanation
High cardinality, meaning many distinct values, is crucial for a good shard key because it enables even distribution of data across shards. A shard key with low cardinality can lead to unbalanced shards where some shards hold much more data than others.
While the shard key field must be indexed, this is automatically handled by MongoDB when you shard the collection. The field does not need to be unique or a string. The most important consideration is ensuring the shard key provides good data distribution and supports your query patterns.
Code Solution
SolutionRead Only
// Good shard key - high cardinality
sh.shardCollection("db.orders", { userId: 1, orderDate: 1 })
// Many unique combinations of userId and orderDate
// Poor shard key - low cardinality
sh.shardCollection("db.orders", { status: 1 })
// Only a few status values like 'pending', 'completed'
// Results in uneven distribution