Skip to main content

What is a shard key (the sharding key)?

A shard key is a field (or set of fields) in a document that MongoDB uses to distribute data across shards. It's exactly what determines which server a given document ends up on.

Why a shard key is needed

  • to split data into pieces
  • to spread the load evenly
  • so queries can run against the shard they need, instead of all of them

The core idea

If you pick a shard key, e.g.:

js
{ shardKey: 1 }

then documents with different shardKey values end up on different shards, rather than one server.

Requirements

A shard key needs to be:

  • evenly distributed (so load doesn't pile onto one shard)
  • used often in queries, so more queries can go to one specific shard (rather than broadcasting to all)

Summary

A shard key is the field that determines how documents get distributed across shards, and the whole sharded cluster's efficiency depends on choosing it well.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.