Problem Statement
What is the main advantage of hashed sharding over range-based sharding?
Explanation
Hashed sharding distributes data based on a hash of the shard key value rather than the actual value. This ensures more even distribution, especially for monotonically increasing keys like timestamps or sequential IDs.
With range-based sharding, monotonically increasing keys cause all new writes to go to the same chunk on the same shard, creating a hotspot. Hashed sharding avoids this by distributing writes evenly across all shards. However, hashed sharding cannot support range queries efficiently because consecutive values are scattered across different shards.
Code Solution
SolutionRead Only
// Range-based sharding (default)
sh.shardCollection("db.orders", { orderId: 1 })
// orderId 1-1000 → Shard 1
// orderId 1001-2000 → Shard 2
// New inserts always go to last shard (hotspot)
// Hashed sharding
sh.shardCollection("db.orders", { orderId: "hashed" })
// Hash of orderId determines shard
// Even distribution, no hotspots
// But range queries scan all shards