Contents
How do I choose a shard key?
The choice of shard key determines three important things:
- The distribution of reads and writes. The most important of these is distribution of reads and writes.
- The size of your chunks. Secondarily important is the chunk size.
- The number of shards each query hits.
- Hashed id.
- Multi-tenant compound index.
How do I change the shard key on an existing MongoDB collection?
In MongoDB 4.2 and earlier, if you must change a shard key after sharding a collection and cannot upgrade, the best option is to:
- dump all data from MongoDB into an external format.
- drop the original sharded collection.
- configure sharding using a more ideal shard key.
What is MongoDB shard key?
The shard key is either a single indexed field or multiple fields covered by a compound index that determines the distribution of the collection’s documents among the cluster’s shards. Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.
How are shard key values distributed in MongoDB?
MongoDB divides the span of shard key values (or hashed shard key values) into non-overlapping ranges of shard key values (or hashed shard key values). Each range is associated with a chunk, and MongoDB attempts to distribute chunks evenly among the shards in the cluster.
Why does MongoDB split documents into smaller chunks?
MongoDB will split large chunks into smaller ones if, and only if, the shard keys are different. If you have too many documents with the same shard key you end up with jumbo chunks.
Why are Jumbo chunks bad for MongoDB?
If you have too many documents with the same shard key you end up with jumbo chunks. Jumbo chunks are bad not only because they cause the data to be unevenly distributed but also because once they grow too large you cannot move them between shards at all. 3.
Why are unique indexes not allowed in MongoDB?
The unique index constraints mean that: For a to-be-sharded collection, you cannot shard the collection if the collection has other unique indexes. For an already-sharded collection, you cannot create unique indexes on other fields.