0
0
MongoDBquery~30 mins

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

Choose your learning style9 modes available
Hash-based Sharding in MongoDB
📖 Scenario: You are managing a large online bookstore database. To handle many users and fast queries, you want to split the book data across multiple servers using hash-based sharding.
🎯 Goal: Set up a MongoDB collection with hash-based sharding on the ISBN field to distribute book documents evenly across shards.
📋 What You'll Learn
Create a MongoDB database called bookstore
Create a collection called books inside bookstore
Enable sharding on the bookstore database
Shard the books collection using a hashed shard key on the ISBN field
Insert sample book documents with ISBN, title, and author
💡 Why This Matters
🌍 Real World
Large-scale applications like online bookstores use sharding to handle huge amounts of data and many users efficiently.
💼 Career
Understanding sharding is important for database administrators and backend engineers working with distributed databases to ensure performance and scalability.
Progress0 / 4 steps
1
Create the bookstore database and books collection
Create a MongoDB database called bookstore and a collection called books. Insert these exact documents into books: { ISBN: "978-0143127741", title: "Sapiens", author: "Yuval Noah Harari" }, { ISBN: "978-0385543767", title: "The Midnight Library", author: "Matt Haig" }, { ISBN: "978-0062316097", title: "The Alchemist", author: "Paulo Coelho" }.
MongoDB
Need a hint?

Use use bookstore to switch to the database. Use db.books.insertMany([...]) to add the documents.

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.

3
Shard the books collection using a hashed shard key on ISBN
Shard the books collection in the bookstore database using a hashed shard key on the ISBN field by running sh.shardCollection("bookstore.books", { ISBN: "hashed" }).
MongoDB
Need a hint?

Use sh.shardCollection() with the full collection name and specify the shard key as { ISBN: "hashed" }.

4
Insert an additional book document after sharding
Insert a new book document into the books collection with these exact values: { ISBN: "978-1501128035", title: "The Power of Habit", author: "Charles Duhigg" }.
MongoDB
Need a hint?

Use db.books.insertOne() with the exact document.