Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What is a shard key in MongoDB, and why is a monotonically increasing key like createdAt or an ObjectId a poor choice for a write-heavy collection?

The shard key is the field or fields that decide which shard stores each document; mongos, the query router, uses it to send each query only to the shards that can hold matching data. With ranged sharding on a monotonically increasing key such as createdAt or an ObjectId, every new document has the highest key so far and lands in the last chunk, which lives on one shard, so that shard takes all inserts while the others sit idle. Hashed sharding spreads inserts but scatters range queries on the key to all shards, and a compound key such as {customerId: 1, createdAt: 1} spreads writes while keeping each customer's data together; queries that do not include the shard key are scatter-gather and scale poorly.

Why this question is asked

Choosing a shard key is the most consequential decision in a sharded MongoDB cluster, because it decides how data and load spread across machines, and changing it later is costly. The most common mistake is the most natural choice: shard by the creation time or the default _id, because every document has one. Interviewers ask this to see whether we understand how documents are placed and why that choice concentrates all writes on one machine. In this answer, we will recap shards, chunks and mongos, see the hot shard happen with an increasing key, look at hashed sharding and what it costs, design a compound key, and finish with how queries are routed.

1A Quick Recap of Sharding

Sharding splits one collection across several replica sets called shards. Each document goes to exactly one shard, chosen by its shard key value. With ranged sharding, the space of key values is divided into contiguous ranges called chunks, for example customerId from 1 to 1,000 on shard A and 1,001 to 2,000 on shard B. The config servers store which chunk lives on which shard.

Applications connect to mongos, a router that looks at each query's filter, finds which chunks can contain matching documents, and sends the query only to their shards. A background process called the balancer moves chunks between shards to keep the data evenly distributed.

2The Hot Shard With an Increasing Key

Suppose we shard an events collection by createdAt with ranged sharding. Every new event has a createdAt later than all existing ones.

Java
sh.shardCollection("app.events", { createdAt: 1 });
db.events.insertOne({ createdAt: ISODate("2026-11-06T10:00:00Z"), ... });
db.events.insertOne({ createdAt: ISODate("2026-11-06T10:00:01Z"), ... });
// every insert falls in the chunk [ 2026-11-05T22:14, MaxKey ) on shard C
Chunks for a createdAt shard key
Chunk rangeShardNew inserts
MinKey to 2026-03-01Anone
2026-03-01 to 2026-07-01Bnone
2026-07-01 to 2026-11-05Anone
2026-11-05 to MaxKeyCall of them
With a monotonically increasing key, write throughput is limited to one shard.

The last chunk always covers the range from the latest split point up to MaxKey, the largest possible value. Every new key is bigger than everything before it, so every insert goes into that one chunk on one shard. That shard does all the write work, its cache fills with fresh data, and the other shards receive no inserts at all. The balancer eventually splits and moves chunks, but the newest chunk keeps receiving writes, so the hot spot just follows it. An ObjectId has the same problem, because its first bytes are a timestamp, so ObjectIds created later sort higher.

3Hashed Sharding and Its Cost

Hashed sharding fixes the write distribution by placing documents according to a hash of the key instead of the key itself. Consecutive createdAt values hash to unrelated numbers, so inserts spread evenly across all shards.

Java
sh.shardCollection("app.events", { _id: "hashed" });
// inserts spread across shards A, B and C evenly
db.events.find({ _id: someId }); // targeted: one shard
db.events.find({ createdAt: { $gte: lastHour } }); // scatter-gather: all shards

The price is range queries. Documents that are next to each other in key order are now on different shards, so a query for 'events in the last hour' must go to every shard and merge the results. Equality lookups on the hashed field are still targeted to one shard. So hashed sharding is a good fit when access is mostly by exact key, such as lookups by id, and a poor fit when the main queries are ranges on the key.

4A Compound Key That Spreads Writes and Keeps Data Together

Often the best key combines a field with many values that spreads load, with a field that orders data within it. For an orders collection where most queries ask for one customer's orders, {customerId: 1, createdAt: 1} is a strong choice.

Java
sh.shardCollection("shop.orders", { customerId: 1, createdAt: 1 });
db.orders.find({ customerId: 42 }).sort({ createdAt: -1 });
// targeted: customer 42's chunk range lives on one shard, sorted by createdAt

Writes spread because many customers are placing orders at the same time, and customers are spread over all chunks. At the same time, all of one customer's orders sit next to each other in key order, usually in one chunk on one shard, so 'this customer's orders' is a targeted query that reads one shard, already sorted by date. A good shard key has high cardinality, many distinct values, low frequency, no single value much more common than others, and is not monotonic in its leading field. customerId satisfies these if no single customer dominates the traffic.

5How Queries Are Routed

mongos can only target a query to specific shards if the filter includes the shard key, or a prefix of it for a compound key. A query that filters by customerId goes to the shard that holds that customer's range. A query that filters only on status, with no customerId, cannot be targeted, so mongos sends it to every shard and merges the results; this is called scatter-gather.

Scatter-gather is fine for rare queries, but it scales badly for frequent ones: with 10 shards, every such query uses resources on all 10, so adding shards does not increase its throughput, and the slowest shard decides its latency. That is why we choose the shard key from the most frequent queries and make sure they include it.

Step throughHow mongos routes a query
  1. Query arrives at mongosfind orders with a filter
  2. Does the filter include the shard key or its prefix?config metadata cached in mongos
    YesLook up matching chunksNoSend to every shard
  3. Send only to shards owning those chunkstargeted query
  4. Merge results and returnsort and limit applied across shards

4 steps in order. Play them, or step through them one at a time.

6Follow-up questions

Can I change the shard key later?

Since MongoDB 4.4 we can refine a shard key by adding suffix fields, and since 5.0 we can reshard a collection to a completely new key with reshardCollection. Both work online but cost time and capacity, so choosing well at the start still matters.

Is an ObjectId always a bad shard key?

As a ranged key on a write-heavy collection, yes, because it increases over time. As a hashed key it spreads writes well and is a reasonable default when queries are mostly by _id.

What if one customer is much larger than all others?

Then customerId has a high-frequency value, and that customer's documents can concentrate on one shard. Adding a second field to the key that varies within the customer lets their data split across chunks.

7Conclusion

In this answer, we saw that the shard key decides which shard stores each document and lets mongos target queries. A monotonically increasing key such as createdAt or an ObjectId sends every insert to the last chunk on one shard, creating a hot shard. Hashed sharding spreads writes but turns range queries on the key into scatter-gather, while a compound key such as {customerId: 1, createdAt: 1} spreads writes across customers and keeps each customer's data together and sorted. Queries without the shard key go to every shard and scale poorly.

What to say in the interviewAbout 90 seconds aloud

Open with

The shard key is the field, or fields, that decide which shard a document lives on. With ranged sharding, the key space is cut into chunks, contiguous ranges of key values, each on a shard, and mongos uses the key in a query's filter to send it only to the shards that can have matching documents.

Then, if they want more

The problem with something like createdAt or an ObjectId, which also starts with a timestamp, is that every new document has the largest key so far. So every insert lands in the last chunk, the one running up to MaxKey, on a single shard.

That shard does all the write work while the others are idle, and splitting and balancing just move the hot spot along. Hashed sharding fixes the distribution, because consecutive values hash to different shards, but then a range query on that key, like the last hour of events, has to go to every shard.

Often the best choice is a compound key like customerId plus createdAt: many customers write at the same time so writes spread, while one customer's orders stay together and sorted, so the common query is targeted.

And I would make sure the frequent queries include the shard key, since queries without it are scatter-gather to every shard and don't scale as shards are added.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
MongoDB manual
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is a shard key in MongoDB, and why is a monotonically increasing key like createdAt...": https://scrivio-interview-library.netlify.app/databases/mongodb/d393a4ca62/