0
0
MongoDBquery~30 mins

Horizontal scaling mental model in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Horizontal scaling mental model
📖 Scenario: Your e-commerce application's orders collection has grown to 500 million documents. A single MongoDB server can no longer handle the read/write load. You need to shard the collection across multiple servers to distribute the data and traffic.
🎯 Goal: Enable sharding on a database, choose an appropriate shard key, shard a collection, and verify the data distribution across shards.
📋 What You'll Learn
Enable sharding on the database
Shard a collection with an appropriate compound key
Check the shard distribution
Configure the balancer window
💡 Why This Matters
🌍 Real World
Sharding is how MongoDB scales to billions of documents across companies like eBay, Forbes, and Uber that need to handle massive data volumes.
💼 Career
Database administrators and backend engineers must understand sharding to design MongoDB deployments that scale with growing data and traffic.
Progress0 / 4 steps
1
Enable sharding on the database
Use sh.enableSharding() to enable sharding on the "ecommerce" database.
MongoDB
Need a hint?

Use sh.enableSharding("databaseName") to enable sharding on a specific database.

2
Create index and shard the collection
First create a compound index on { "region": 1, "orderId": 1 } for the ecommerce.orders collection using db.orders.createIndex(). Then use sh.shardCollection() to shard "ecommerce.orders" on that same compound key.
MongoDB
Need a hint?

The shard key must have a supporting index. Create the index first with db.orders.createIndex(), then shard with sh.shardCollection().

3
Check shard distribution
Use db.orders.getShardDistribution() to check how data is distributed across the shards.
MongoDB
Need a hint?

Use db.orders.getShardDistribution() to see the percentage of data on each shard.

4
Configure balancer window
Set the balancer to only run during off-peak hours. Use db.settings.updateOne() on the config database to set the balancer's activeWindow with start as "02:00" and stop as "06:00". Filter by { _id: "balancer" } and use $set.
MongoDB
Need a hint?

Update the balancer settings in db.settings with an activeWindow object containing start and stop times.