Catalog My learning Blog
MongoDB Tutorial: Schema Design, Indexing and Sharding · Phase 4 · Lesson 23 · 3 min read

How to Choose a MongoDB Shard Key

On this page
  1. Why the shard key matters
  2. Four properties of a good shard key
  3. Evaluating shard key candidates for orders
  4. Shard key patterns that work
  5. Testing a shard key with analyzeShardKey
  6. Check yourself

Why the shard key matters

A shard key decides where every document lives, which shard every insert hits, and whether each query touches one shard or all of them. A poor key turns a ten-shard cluster into one overloaded shard and nine idle ones. You can reshard later (Day 22), but it's slow and resource-hungry, so choose carefully up front.

Four properties of a good shard key

Cardinality: how many distinct values? A key with few values (like country or status) can only be divided into that many chunks. Ten values means at most ten chunks: data can't spread across more shards, and each chunk can grow into an unsplittable jumbo chunk.

Frequency: how evenly are values used? Even with many distinct values, if one value is very common (one marketplace seller with 30 percent of orders), its documents pile into one chunk on one shard.

Monotonic growth: do new values always increase? Timestamps, auto-increment ids and default ObjectIds grow constantly. With ranged sharding, every new document goes into the chunk holding the maximum value, on one shard: a hot shard absorbing all inserts while the others idle.

Query fit: do the main queries include it? Queries with the shard key are targeted to one shard; without it, they scatter to all (Day 22).

A good shard key has high cardinality, even frequency and no monotonic growth, and appears in the most frequent queries.
A good shard key has high cardinality, even frequency and no monotonic growth, and appears in the most frequent queries.

Evaluating shard key candidates for orders

Main queries: a customer's orders (most frequent), a single order by id, and occasional admin searches by date.

CandidateCardinalityFrequencyMonotonic?Query fitVerdict
status4 valuesVery skewedNoPoorUnusable
createdAt (ranged)HighEvenYes: hot shardDate ranges onlyPoor
{ _id: "hashed" }HighEvenHashed, so noSingle-order lookups only; customer queries scatterWorks for writes, weak for reads
customerIdHighSkewed by big customersNoCustomer queries targetedGood, unless a few customers are huge
{ customerId: 1, _id: 1 }Very highBig customers split across chunksNoCustomer queries targetedBest fit

The compound key keeps a customer's orders together (so "my orders" hits one shard, or a few for a huge customer), while the _id suffix lets a very large customer's orders split into many chunks instead of forming a jumbo chunk.

Shard key patterns that work

  • Compound key: a query field plus a high-cardinality suffix, as above. Most real shard keys look like this.
  • Hashed key when queries are mostly by exact id and writes must spread perfectly evenly, such as event ingestion by deviceId or a hashed _id.
  • Multi-tenant SaaS: { tenantId: 1, _id: 1 }, sometimes with zones to pin large tenants to dedicated shards.

Testing a shard key with analyzeShardKey

  • Analyze real query logs: which fields appear in the most frequent and most important queries?
  • Check value distribution: db.orders.aggregate([{ $group: { _id: "$customerId", n: { $sum: 1 } } }, { $sort: { n: -1 } }, { $limit: 10 }]) reveals heavy keys.
  • MongoDB 7.0+ has analyzeShardKey, which reports cardinality, frequency, monotonicity and how many sampled queries would be targeted for a candidate key.
  • Load test with a production-like distribution before going live.

Check yourself

It increases constantly, so every new document goes to the chunk with the highest values, on one shard, creating a hot shard.

It raises the key's cardinality, so a very large customer's documents can be split across many chunks and shards instead of piling into one unsplittable jumbo chunk, while queries by customerId still target only the shards holding that customer.

analyzeShardKey, which measures a candidate key's cardinality, frequency and monotonicity from the data, and, with query sampling enabled, how well real queries would be targeted. It lets you test keys before resharding.

Yes, since MongoDB 5.0 with reshardCollection, but it copies the whole collection and is heavy. Refining a key by adding suffix fields is cheaper. Choose carefully up front.

Each value can only be one chunk range, so a few countries with lots of data create huge chunks that can't be split or spread across shards.