Sharding is MongoDB's mechanism for horizontal scaling — distributing data across multiple servers (shards). Each shard is a replica set. The cluster has three components:
- mongos: Query router — clients connect to mongos (not shards directly). Routes queries to correct shard(s). Stateless; multiple can run in parallel.
- Config Servers: Store the cluster's metadata — which chunks of data are on which shard. Always a replica set (3 nodes). Critical — loss of all config servers stops the cluster.
- Shards: Each shard is a replica set holding a subset of the data. Add more shards to scale out storage and write throughput.
// Connect to mongos (not directly to shard mongod) mongosh "mongodb://mongos1:27017,mongos2:27017/mydb" // Enable sharding on a database sh.enableSharding("mydb") // Shard a collection (must happen before data is inserted for best distribution) sh.shardCollection("mydb.orders", { customerId: 1, createdAt: 1 }) // Check sharding status sh.status() // overview of shards, databases, collections, chunks db.printShardingStatus()