How do you choose sharding key?

How do you choose sharding key?

What makes a good shard key?

  1. The distribution of reads and writes. The most important of these is distribution of reads and writes.
  2. The size of your chunks. Secondarily important is the chunk size.
  3. The number of shards each query hits.
  4. Hashed id.
  5. Multi-tenant compound index.

What is shard key MongoDB?

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.

Is MongoDB key-value?

Is MongoDB a key-value database? MongoDB is a document database, which means that it stores data in the form of “objects” which have properties which can be changed, added to, deleted, and queried against.

What should I do if I need to change my shard key in MongoDB?

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. pre-split the shard key range to ensure initial even distribution.

What should I do after Sharding a collection in MongoDB?

If you must change a shard key after sharding a collection, 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. pre-split the shard key range to ensure initial even distribution. restore the dumped data into MongoDB.

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 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.