0
0
MongoDBquery~10 mins

Range-based sharding in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Range-based sharding
Start: Insert Document
Extract Shard Key Value
Compare Value to Ranges
Find Matching Shard Range
Route Document to Shard
Store Document in Shard
End
Documents are routed to shards by comparing their shard key value to defined ranges, then stored in the matching shard.
Execution Sample
MongoDB
sh.enableSharding("mydb")
sh.shardCollection("mydb.users", { age: 1 })
// Define ranges:
// Shard1: age < 30
// Shard2: 30 <= age < 60
// Shard3: age >= 60

// Insert document {name: "Alice", age: 25}
This example enables sharding on a collection by age, defines ranges for shards, and inserts a document to be routed.
Execution Table
StepActionShard Key ValueRange CheckedShard SelectedResult
1Insert document {name: "Alice", age: 25}25age < 30Shard1Document routed to Shard1
2Insert document {name: "Bob", age: 45}4530 <= age < 60Shard2Document routed to Shard2
3Insert document {name: "Carol", age: 65}65age >= 60Shard3Document routed to Shard3
4Insert document {name: "Dave", age: 30}3030 <= age < 60Shard2Document routed to Shard2
5Insert document {name: "Eve", age: 29}29age < 30Shard1Document routed to Shard1
6No more documents---End of routing
💡 All documents routed to shards based on their age falling into defined ranges.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
Shard Key Value-2545653029-
Shard Selected-Shard1Shard2Shard3Shard2Shard1-
Key Moments - 2 Insights
Why does the document with age 30 go to Shard2 and not Shard1?
Because the range for Shard1 is age < 30, so 30 is not included there. The range for Shard2 is 30 <= age < 60, so 30 fits in Shard2's range as shown in execution_table row 4.
What happens if a document's shard key value does not fit any defined range?
In range-based sharding, all possible values should be covered by ranges. If not, the document cannot be routed properly. This example assumes full coverage, so no such case appears in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, which shard does the document with age 65 get routed to?
AShard3
BShard1
CShard2
DNone
💡 Hint
Check row 3 in the execution_table where age 65 is routed.
At which step does the shard key value equal 30?
AStep 2
BStep 4
CStep 5
DStep 1
💡 Hint
Look at the Shard Key Value column in execution_table.
If the range for Shard1 changed to age <= 30, where would the document with age 30 be routed?
AShard3
BShard2
CShard1
DCannot be determined
💡 Hint
Consider how the range comparison changes and check the routing logic in concept_flow.
Concept Snapshot
Range-based sharding routes documents by shard key value ranges.
Define continuous, non-overlapping ranges for shards.
Insertions compare shard key to ranges to pick shard.
Example: age < 30 → Shard1, 30 ≤ age < 60 → Shard2, age ≥ 60 → Shard3.
Ensure all values fit some range to avoid routing errors.
Full Transcript
Range-based sharding in MongoDB splits data by ranges of a shard key. When a document is inserted, the system looks at the shard key value and compares it to the defined ranges. It finds which range the value fits in and sends the document to the corresponding shard. For example, if the shard key is age, and ranges are set as age less than 30 for Shard1, age between 30 and 59 for Shard2, and age 60 or more for Shard3, then a document with age 25 goes to Shard1, age 45 to Shard2, and age 65 to Shard3. This method helps keep related data together and balances load. It is important that the ranges cover all possible values so every document can be routed correctly.