MongoDB sharding distributes large datasets across multiple servers to ensure horizontal scaling and high availability. Below are the essential commands to set up, manage, and monitor a sharded cluster.
1. Enable Sharding for a Database
Before sharding a collection, sharding must be enabled at the database level.
sh.enableSharding("myDatabase")
This enables sharding for the myDatabase
.
2. Shard a Collection
To shard a collection, choose a shard key and run:
sh.shardCollection("myDatabase.myCollection", { "userId": "hashed" })
This shards myCollection
in myDatabase
using the userId
field with hashed sharding.
3. Check Shard Status
Monitor the status of the sharded cluster:
sh.status()
This displays all shards, chunks, and data distribution.
4. Add a New Shard to the Cluster
If more capacity is needed, add a new shard:
sh.addShard("mongodb://shard1:27017")
sh.addShard("mongodb://shard2:27017")
This adds shard1
and shard2
to the cluster.
5. List All Shards in the Cluster
To see the current shards in your MongoDB cluster:
sh.getShards()
This returns a list of all active shards.
6. Balance Data Between Shards
MongoDB automatically balances shards, but you can manually start balancing:
sh.startBalancer()
Enables automatic chunk balancing.
To stop the balancer (useful during maintenance):
sh.stopBalancer()
7. Move a Chunk to a Specific Shard
To manually move a chunk of data to a different shard:
sh.moveChunk("myDatabase.myCollection", { userId: 5000 }, "shard2")
Moves the chunk containing userId: 5000
to shard2
.
8. Split a Chunk
To improve query efficiency, you can manually split a chunk:
sh.splitAt("myDatabase.myCollection", { userId: 10000 })
This splits a chunk at userId: 10000
to distribute data efficiently.
9. Merge Two Chunks
If chunks are too small, you can merge them:
sh.mergeChunks("myDatabase.myCollection", { userId: 5000 })
This merges adjacent chunks around userId: 5000
.
10. Remove a Shard from the Cluster
If a shard is no longer needed, remove it safely:
sh.removeShard("shard1")
This moves data from shard1
to other shards and removes it.
11. View Data Distribution Across Shards
To check how data is distributed across shards:
db.myCollection.getShardDistribution()
This shows document count per shard.
12. Disable Sharding on a Collection (Only in Testing)
In rare cases, you may need to remove sharding from a collection:
sh.disableBalancing("myDatabase.myCollection")
This does not remove sharding but stops rebalancing.
Summary of Key MongoDB Sharding Commands
Action | Command |
---|---|
Enable sharding | sh.enableSharding("myDatabase") |
Shard a collection | sh.shardCollection("myDatabase.myCollection", { "userId": "hashed" }) |
Check sharding status | sh.status() |
Add a shard | sh.addShard("mongodb://shard1:27017") |
List shards | sh.getShards() |
Start balancing | sh.startBalancer() |
Stop balancing | sh.stopBalancer() |
Move a chunk | sh.moveChunk("myDatabase.myCollection", { userId: 5000 }, "shard2") |
Split a chunk | sh.splitAt("myDatabase.myCollection", { userId: 10000 }) |
Merge chunks | sh.mergeChunks("myDatabase.myCollection", { userId: 5000 }) |
Remove a shard | sh.removeShard("shard1") |
View shard distribution | db.myCollection.getShardDistribution() |
Conclusion
MongoDB sharding ensures scalability by distributing data across multiple servers. Using these commands, you can configure, manage, and optimize sharding for high-performance applications.