0
0
MongoDBquery~30 mins

Choosing a good shard key in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Choosing a Good Shard Key in MongoDB
📖 Scenario: You are managing a MongoDB database for an online bookstore. The database stores information about books, including their _id, title, author, genre, and copies_sold. The database is growing fast, and you want to distribute the data across multiple servers to improve performance and scalability.To do this, you need to choose a good shard key for the books collection. A shard key is a field that MongoDB uses to split the data into smaller parts called shards. Choosing the right shard key helps the database find and store data efficiently.
🎯 Goal: In this project, you will create a books collection with sample data, define a shard key, and apply sharding to the collection. You will learn how to pick a shard key that balances the data well and supports common queries.
📋 What You'll Learn
Create a books collection with 5 sample documents with exact fields and values
Define a shard key variable called shardKey with the field name to shard on
Enable sharding on the database and shard the books collection using the shardKey
Use the sh.shardCollection() command with the correct shard key format
💡 Why This Matters
🌍 Real World
Sharding helps large databases handle more data and users by splitting data across servers. Choosing a good shard key is important to keep data balanced and queries fast.
💼 Career
Database administrators and backend engineers often configure sharding to scale applications. Understanding shard keys is key to managing big data systems effectively.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a books collection with these exact 5 documents: { _id: 1, title: "The Great Gatsby", author: "F. Scott Fitzgerald", genre: "Classic", copies_sold: 25000000 }, { _id: 2, title: "1984", author: "George Orwell", genre: "Dystopian", copies_sold: 30000000 }, { _id: 3, title: "To Kill a Mockingbird", author: "Harper Lee", genre: "Classic", copies_sold: 40000000 }, { _id: 4, title: "The Hobbit", author: "J.R.R. Tolkien", genre: "Fantasy", copies_sold: 100000000 }, { _id: 5, title: "Harry Potter and the Sorcerer's Stone", author: "J.K. Rowling", genre: "Fantasy", copies_sold: 120000000 }.
MongoDB
Need a hint?

Use db.books.insertMany() with an array of objects matching the exact fields and values.

2
Define the shard key field
Create a variable called shardKey and set it to the string "genre" to use the genre field as the shard key.
MongoDB
Need a hint?

Use const shardKey = "genre" to define the shard key field.

3
Enable sharding on the database
Run the command sh.enableSharding("bookstore") to enable sharding on the bookstore database.
MongoDB
Need a hint?

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

4
Shard the books collection using the shard key
Run the command sh.shardCollection("bookstore.books", { [shardKey]: 1 }) to shard the books collection on the genre field in ascending order.
MongoDB
Need a hint?

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