0
0
MongodbHow-ToBeginner · 4 min read

How to Shard Collection in MongoDB: Step-by-Step Guide

To shard a collection in MongoDB, first enable sharding on the database using sh.enableSharding(), then shard the collection with sh.shardCollection() by specifying the collection name and shard key. This distributes data across shards for horizontal scaling.
📐

Syntax

Sharding in MongoDB involves two main commands:

  • sh.enableSharding(<database>): Enables sharding on the specified database.
  • sh.shardCollection(<namespace>, { <shardKey> : 1 }): Shards the collection by the given shard key.

The namespace is the full name of the collection in the format database.collection. The shardKey is a field or fields used to distribute data across shards.

mongodb
sh.enableSharding("myDatabase")
sh.shardCollection("myDatabase.myCollection", { userId: 1 })
💻

Example

This example shows how to enable sharding on a database called shop and shard the orders collection using the orderId field as the shard key.

mongodb
sh.enableSharding("shop")
sh.shardCollection("shop.orders", { orderId: 1 })
Output
{ "ok" : 1 } { "collectionsharded" : "shop.orders", "key" : { "orderId" : 1 }, "ok" : 1 }
⚠️

Common Pitfalls

Common mistakes when sharding collections include:

  • Not enabling sharding on the database before sharding a collection.
  • Choosing a shard key with low cardinality or that does not distribute data evenly, causing unbalanced shards.
  • Sharding collections that are too small or do not require horizontal scaling.
  • Attempting to shard collections without an index on the shard key.

Always create an index on the shard key before sharding the collection.

mongodb
/* Wrong: Shard collection without enabling sharding on database */
sh.shardCollection("shop.orders", { orderId: 1 })

/* Right: Enable sharding first */
sh.enableSharding("shop")
sh.shardCollection("shop.orders", { orderId: 1 })
📊

Quick Reference

CommandDescription
sh.enableSharding()Enable sharding on a database
sh.shardCollection(, { : 1 })Shard a collection by shard key
db.collection.createIndex({ : 1 })Create index on shard key before sharding
sh.status()Check current sharding status

Key Takeaways

Always enable sharding on the database before sharding any collection.
Choose a shard key that evenly distributes data to avoid unbalanced shards.
Create an index on the shard key before sharding the collection.
Use sh.shardCollection() with the full collection namespace and shard key.
Check sharding status with sh.status() to monitor your cluster.