0
0
MongoDBquery~30 mins

Range-based sharding in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Range-based Sharding in MongoDB
📖 Scenario: You are managing a large online bookstore database. To handle the growing number of book sales efficiently, you want to distribute the sales data across multiple servers based on the sale date. This technique is called range-based sharding.Range-based sharding splits data into chunks based on a range of shard key values. Here, the shard key will be the saleDate field.
🎯 Goal: Set up a MongoDB sharded collection called bookSales in the bookstore database. Configure range-based sharding on the saleDate field to distribute sales data by date ranges.
📋 What You'll Learn
Create the bookSales collection with sample sales documents.
Enable sharding on the bookstore database.
Shard the bookSales collection using saleDate as the shard key.
Split the data into at least two chunks based on saleDate ranges.
💡 Why This Matters
🌍 Real World
Range-based sharding helps large databases distribute data efficiently by ranges, improving query performance and scalability.
💼 Career
Database administrators and backend engineers use sharding to manage big data and ensure fast access in production systems.
Progress0 / 4 steps
1
Create the bookSales collection with sample data
In the bookstore database, create a collection called bookSales and insert these exact documents: { _id: 1, book: "MongoDB Basics", saleDate: ISODate("2023-01-15") }, { _id: 2, book: "Advanced MongoDB", saleDate: ISODate("2023-03-10") }, and { _id: 3, book: "MongoDB Performance", saleDate: ISODate("2023-06-05") }.
MongoDB
Need a hint?

Use db.collection.insertMany() to add multiple documents at once.

2
Enable sharding on the bookstore database
Enable sharding on the bookstore database by running sh.enableSharding("bookstore").
MongoDB
Need a hint?

Use the sh.enableSharding() command with the database name as a string.

3
Shard the bookSales collection on saleDate
Shard the bookSales collection in the bookstore database using saleDate as the shard key by running sh.shardCollection("bookstore.bookSales", { saleDate: 1 }).
MongoDB
Need a hint?

Use sh.shardCollection() with the full namespace and shard key object.

4
Split the bookSales collection into chunks by saleDate
Create a split point at ISODate("2023-04-01") to divide the bookSales collection into two chunks by running sh.splitAt("bookstore.bookSales", { saleDate: ISODate("2023-04-01") }).
MongoDB
Need a hint?

Use sh.splitAt() with the full namespace and the split key value.