MongoDB mongos

Intermediate
6 min read
Updated: Nov 2026
Getting Started

What Is mongos?

In a sharded cluster, applications do not talk to shards directly. They connect to mongos, a lightweight router process. For each request, mongos reads cluster metadata (cached from the config servers) to find which shards hold the relevant data, forwards the operation, and merges results into a single response. To your application, a sharded cluster looks like one database.

Why It Matters

ReasonBenefit
Hides complexityYour application code does not need to know where data is stored.
Scales independentlyAdd more mongos instances as client load grows.
Efficient routingTargeted queries avoid touching irrelevant shards.
Single entry pointAdmins run sharding commands in one place.

How To Use It

  1. Start the config server replica set (CSRS) and the shard replica sets first.
  2. Start mongos with --configdb pointing at the config server replica set.
  3. Connect to it with mongosh on port 27017.
  4. Add shards with sh.addShard() using each replica set's connection string.
  5. Enable sharding on a database and shard a collection with sh.shardCollection().
  6. Point your application connection string at one or more mongos instances.
  7. Use explain() to see whether queries are targeted or hit all shards.

mongosh Examples

shell / mongosh
# Start a router pointing at the config server replica set
mongos --configdb csrs/cfg1:27019,cfg2:27019,cfg3:27019 --bind_ip localhost,router1 --port 27017

// In mongosh connected to mongos
sh.addShard("shardA/a1:27018,a2:27018,a3:27018")
sh.addShard("shardB/b1:27018,b2:27018,b3:27018")

sh.enableSharding("shop")
sh.shardCollection("shop.orders", { customerId: "hashed" })

sh.status()

// Targeted vs scatter-gather: check the plan's shards
db.getSiblingDB("shop").orders.find({ customerId: 42 }).explain()

// Application connection string with two routers
// mongodb://router1:27017,router2:27017/shop

Edge Cases & Pitfalls

Do not connect to shards directly

Writing to a shard bypassing mongos can create inconsistent data. Always go through the router.

Single mongos is a single point of failure

Run at least two for availability, and list them in the connection string.

Queries without the shard key are costly

They hit every shard, and the merge work happens on mongos.

Metadata cache

mongos caches routing information; it refreshes automatically when chunks move.

Cheat Sheet

TopicDetail
RoleRoutes client operations to the correct shard(s) and merges results
StatelessDoes not store data; caches metadata from config servers
Default port27017
Targeted queryFilter includes the shard key, so only some shards are contacted
Broadcast (scatter-gather)Without the shard key, the query goes to all shards
Multiple mongosRun several for scale and availability; clients list them in the URI
Startmongos --configdb csrs/host1:27019,host2:27019,host3:27019
sh.status()Run from a mongos to view cluster layout
Connection stringmongodb://mongos1:27017,mongos2:27017/shop
Admin commandsSharding commands such as sh.shardCollection run through mongos

Key Takeaways

Connect applications to one or more mongos instances, never to individual shards. Include the shard key in queries so mongos can target specific shards.

Frequently Asked Questions

What is mongos?

The query router for a sharded cluster. It receives client requests, sends them to the right shards, and returns merged results.

Does mongos store data?

No. It is stateless apart from a cache of cluster metadata from the config servers.

How many mongos instances do I need?

At least two for availability in production. You can run one per application server or a pool behind a load balancer, since the driver can connect to many.

How does mongos know where data lives?

It reads chunk and shard metadata stored on the config servers and caches it.

What is a scatter-gather query?

A query without the shard key that mongos must send to every shard and then combine.

Can I connect to a shard directly?

You can technically, but you should not for application work. Use mongos so routing and consistency are handled correctly.

Next: MongoDB Config Servers

Continue the MongoDB learning path.

MongoDB Config Servers →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful