0
0
MongoDBquery~30 mins

Mongos router behavior in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Mongos Router Behavior in MongoDB
📖 Scenario: You are managing a MongoDB sharded cluster for an online bookstore. The cluster uses a mongos router to direct queries to the correct shards. You want to understand how mongos routes queries and manages data distribution.
🎯 Goal: Build a simple MongoDB setup with a sharded collection and use mongos to query data. Learn how mongos routes queries to shards based on shard keys.
📋 What You'll Learn
Create a sharded collection with a shard key
Enable sharding on the database
Insert sample documents into the collection
Query the collection through the mongos router
💡 Why This Matters
🌍 Real World
Sharded MongoDB clusters are used in real-world applications to scale databases horizontally and distribute data across multiple servers for better performance and availability.
💼 Career
Understanding mongos router behavior is essential for database administrators and backend developers working with large-scale MongoDB deployments to optimize query performance and data distribution.
Progress0 / 4 steps
1
Enable sharding on the bookstore database
Use the sh.enableSharding() command to enable sharding on the database named bookstore.
MongoDB
Need a hint?

Use sh.enableSharding("bookstore") to enable sharding on the database.

2
Shard the books collection on the isbn field
Use the sh.shardCollection() command to shard the collection books in the bookstore database on the isbn field.
MongoDB
Need a hint?

Use sh.shardCollection("bookstore.books", { isbn: 1 }) to shard the collection on the isbn field.

3
Insert sample book documents into the books collection
Insert three documents into the books collection with these exact fields and values: { isbn: "978-0132350884", title: "Clean Code", author: "Robert C. Martin" }, { isbn: "978-0201616224", title: "The Pragmatic Programmer", author: "Andrew Hunt" }, and { isbn: "978-0131103627", title: "The C Programming Language", author: "Brian W. Kernighan" }.
MongoDB
Need a hint?

Use db.books.insertMany([...]) with the exact documents to insert multiple books.

4
Query the books collection for a book by isbn using mongos
Write a query using db.books.find() to find the document where isbn is "978-0201616224". This query will be routed by mongos to the correct shard.
MongoDB
Need a hint?

Use db.books.find({ isbn: "978-0201616224" }) to query the book by its ISBN.