0Pricing
MongoDB Academy · Lesson

Ranged vs Hashed Sharding Strategies

Learners will configure a collection with ranged sharding for range queries or hashed sharding for uniform write distribution and compare their trade-offs.

Two Sharding Strategies Compared

MongoDB supports two built-in sharding strategies: ranged sharding and hashed sharding. Ranged sharding assigns contiguous ranges of shard key values to specific shards; hashed sharding applies a hash function to the key first and distributes based on the hash. Both have distinct strengths, and the right choice depends on your data access patterns.

Ranged Sharding: How It Works

In ranged sharding, MongoDB divides the shard key's value space into contiguous ranges and assigns each range (chunk) to a shard. For example, users with userId 1–10,000 go to shard A, 10,001–20,000 to shard B, and so on. Documents with nearby shard key values are co-located on the same shard, which is ideal for range queries.

// Enable ranged sharding on a field
sh.shardCollection('mydb.products', { category: 1, price: 1 })

// Range query is now targeted to the shard(s) holding that range
db.products.find({ category: 'electronics', price: { $lt: 100 } })

All lessons in this course

  1. Sharding Concepts: Chunks, Balancer, and Shard Keys
  2. Choosing a Shard Key: Cardinality, Frequency, Monotonicity
  3. Ranged vs Hashed Sharding Strategies
  4. Zone Sharding: Pinning Data to Regions
← Back to MongoDB Academy