MongoDB — Sharding
What is Sharding?
Sharding distributes data across multiple machines.
Components
| Component | Description |
|---|---|
| Shard | Stores data |
| Config | Metadata |
| Router | Query routing |
Enable Sharding
// Enable on database
sh.enableSharding("mydb")
// Shard collection
sh.shardCollection("mydb.users", { userId: "hashed" })
Shard Key
| Type | Description |
|---|---|
| Hashed | Even distribution |
| Ranged | Range-based |
Check Status
sh.status()
Mini Practice
- Enable sharding
- Choose shard key
- Check shard status
- Monitor performance
Up Next
Continue with Atlas — MongoDB Atlas cloud service.
Related Topics
Frequently Asked Questions about Sharding
What is Sharding in MongoDB?
Sharding is a fundamental concept in MongoDB. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Sharding?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Sharding.
Why is Sharding important in MongoDB?
Sharding is essential for MongoDB development. Understanding this concept will help you write better code and solve real-world problems more effectively.