Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

MongoDB Sharding

What is Sharding?

Sharding is MongoDB's approach to horizontal scaling. Instead of adding more resources to a single server (vertical scaling), sharding distributes data across multiple servers called shards. Each shard holds a subset of the data, and together they form a single logical database.

A sharded cluster consists of three components:

  • Shards: Each shard is a replica set that stores a portion of the data.
  • mongos (Query Router): Routes client requests to the appropriate shard(s).
  • Config Servers: Store cluster metadata and shard key ranges (deployed as a replica set).

Shard Key Selection

The shard key determines how data is distributed across shards. Choosing the right shard key is critical — a poor choice leads to uneven distribution (hotspots) or scatter-gather queries that hit all shards.

StrategyHow it WorksBest For
Range-basedDocuments with adjacent shard key values go to the same shardRange queries on the shard key
Hash-basedA hash of the shard key value determines the shardEven distribution, write-heavy workloads
Zone shardingAssign specific shard key ranges to specific shardsGeographic data locality, compliance
Enabling Sharding and Sharding a Collection
// Connect to mongos router
mongosh "mongodb://mongos-host:27017"

// Enable sharding on a database
sh.enableSharding("myapp")

// Shard a collection with a hashed shard key (even distribution)
sh.shardCollection("myapp.users", { userId: "hashed" })

// Shard a collection with a range-based compound shard key
sh.shardCollection("myapp.orders", { customerId: 1, createdAt: 1 })

// Check sharding status
sh.status()

// Check which shard a document would go to
db.users.explain().find({ userId: "user123" })
Monitoring and Zone Sharding
// View chunk distribution across shards
use config
db.chunks.find({ ns: "myapp.users" }).pretty()

// Check balancer status
sh.getBalancerState()
sh.isBalancerRunning()

// Zone sharding — route data to specific shards by region
// Assign a zone to a shard
sh.addShardTag("shard0", "US")
sh.addShardTag("shard1", "EU")

// Define zone ranges for the shard key
sh.addTagRange(
  "myapp.users",
  { region: "US", userId: MinKey },
  { region: "US", userId: MaxKey },
  "US"
)
sh.addTagRange(
  "myapp.users",
  { region: "EU", userId: MinKey },
  { region: "EU", userId: MaxKey },
  "EU"
)

// View shard distribution
db.adminCommand({ listShards: 1 })
Shard Key Best Practices
// GOOD shard key characteristics:
// - High cardinality (many unique values)
// - Even distribution of writes
// - Frequently used in queries (avoids scatter-gather)

// BAD shard key examples:
// { status: 1 }       — low cardinality (only a few values), creates hotspots
// { createdAt: 1 }    — monotonically increasing, all writes go to one shard
// { _id: 1 }          — ObjectId is monotonically increasing (use hashed instead)

// GOOD shard key examples:
// { userId: "hashed" }              — even distribution for user data
// { customerId: 1, orderId: 1 }     — compound, good for customer queries
// { email: "hashed" }               — even distribution

// Note: Shard key is IMMUTABLE after sharding
// You cannot change a document's shard key value (MongoDB 4.2+ allows it with limitations)
// Choose carefully before sharding!

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.