How to Choose a MongoDB Shard Key
On this page
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).
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.
| Candidate | Cardinality | Frequency | Monotonic? | Query fit | Verdict |
|---|---|---|---|---|---|
status | 4 values | Very skewed | No | Poor | Unusable |
createdAt (ranged) | High | Even | Yes: hot shard | Date ranges only | Poor |
{ _id: "hashed" } | High | Even | Hashed, so no | Single-order lookups only; customer queries scatter | Works for writes, weak for reads |
customerId | High | Skewed by big customers | No | Customer queries targeted | Good, unless a few customers are huge |
{ customerId: 1, _id: 1 } | Very high | Big customers split across chunks | No | Customer queries targeted | Best 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
deviceIdor 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.