When selecting a shard key for a new collection, which TWO of the following rules must be followed?
Trap 1: The shard key value cannot be null for any document.
MongoDB allows shard key values to be null. If a document lacks the shard key field, it is indexed as null and placed in the chunk corresponding to the null range. While technically allowed, having many null values can lead to 'jumbo chunks' and poor data distribution across the cluster.
Trap 2: The shard key must be the same as the _id field for all collections.
While the _id field can be used as a shard key, it is not a requirement. Developers often choose a different field that better reflects the application's query patterns. Using _id as the shard key provides uniqueness but may not offer the best query targeting or write distribution for every workload.
Trap 3: Shard keys must be defined as unique indexes at the cluster level.
A shard key does not have to be unique unless the application requires it. If you want a unique constraint on a sharded collection, the shard key must be a prefix of the unique index. Otherwise, MongoDB cannot enforce uniqueness across multiple shards because it would require expensive cross-shard communication.
- A
The shard key must be an indexed field or the prefix of an index.
MongoDB requires an index that starts with the shard key to exist before the collection can be sharded. This index allows the cluster to perform range lookups and manage chunk boundaries efficiently. If no such index exists, the shardCollection command will fail to execute until the index is created.
- B
The shard key must be a field that exists in every document.
Every document in a sharded collection must contain the shard key. If a document is missing the shard key field, MongoDB treats the value as null for sharding purposes. Consistent presence of the key ensures that every document can be mapped to a specific chunk and shard in the cluster.
- C
The shard key value cannot be null for any document.
Why it fails: MongoDB allows shard key values to be null. If a document lacks the shard key field, it is indexed as null and placed in the chunk corresponding to the null range. While technically allowed, having many null values can lead to 'jumbo chunks' and poor data distribution across the cluster.
- D
The shard key must be the same as the _id field for all collections.
Why it fails: While the _id field can be used as a shard key, it is not a requirement. Developers often choose a different field that better reflects the application's query patterns. Using _id as the shard key provides uniqueness but may not offer the best query targeting or write distribution for every workload.
- E
Shard keys must be defined as unique indexes at the cluster level.
Why it fails: A shard key does not have to be unique unless the application requires it. If you want a unique constraint on a sharded collection, the shard key must be a prefix of the unique index. Otherwise, MongoDB cannot enforce uniqueness across multiple shards because it would require expensive cross-shard communication.