What is the primary role of a mongos in a MongoDB sharded cluster?
The mongos acts as a query router. It directs client requests to the correct shard(s) based on the cluster's metadata, making the sharding transparent to the client.
Click to reveal answer
intermediate
How does mongos know which shard to send a query to?
Mongos uses the cluster metadata stored in the config servers to determine the shard that holds the data for the query's shard key range.
Click to reveal answer
beginner
True or False: mongos stores data itself in a sharded cluster.
False. Mongos does not store data. It only routes queries to the appropriate shards which hold the actual data.
Click to reveal answer
advanced
What happens if a mongos instance loses connection to the config servers?
Mongos cannot route queries correctly without config server metadata. It will fail to process queries until it reconnects and refreshes metadata.
Click to reveal answer
intermediate
Why might you run multiple mongos instances in a sharded cluster?
Running multiple mongos instances improves availability and load balancing for client requests, preventing a single point of failure or bottleneck.
Click to reveal answer
What is the main function of a mongos router in MongoDB?
ABackup shard data
BRoute queries to the correct shard
CManage config server metadata
DStore data in the cluster
✗ Incorrect
Mongos routes queries to the correct shard based on metadata; it does not store data or manage backups.
Where does mongos get the metadata to route queries?
AFrom the config servers
BFrom the shards directly
CFrom the client application
DFrom the mongod instances
✗ Incorrect
Mongos retrieves cluster metadata from the config servers to know where data is located.
If mongos loses connection to config servers, what happens?
AIt continues routing queries normally
BIt stores data locally
CIt automatically becomes a config server
DIt cannot route queries correctly
✗ Incorrect
Without config server metadata, mongos cannot route queries correctly.
Does mongos store any data in a sharded cluster?
ANo, it only routes queries
BYes, it stores metadata only
CYes, it stores all data
DNo, it stores backups
✗ Incorrect
Mongos only routes queries; data is stored on shards.
Why run multiple mongos instances?
ATo replace config servers
BTo store more data
CTo improve query routing availability
DTo shard data
✗ Incorrect
Multiple mongos instances improve availability and load balancing for query routing.
Explain how mongos routes a client query in a MongoDB sharded cluster.
Think about how mongos acts like a traffic controller.
You got /4 concepts.
Describe what happens if a mongos instance cannot access the config servers.
Consider the importance of config server metadata for routing.
You got /4 concepts.
Practice
(1/5)
1. What is the primary role of the mongos router in a sharded MongoDB cluster?
easy
A. It stores the actual data for the database.
B. It routes queries to the correct shard based on the shard key.
C. It manages user authentication and roles.
D. It backs up the database automatically.
Solution
Step 1: Understand mongos role
The mongos acts as a query router in a sharded cluster, directing queries to the right shard.
Step 2: Differentiate from other components
Data storage is handled by shards, not mongos. Authentication and backups are separate concerns.
Final Answer:
It routes queries to the correct shard based on the shard key. -> Option B
Quick Check:
mongos routes queries [OK]
Hint: Remember: mongos routes, shards store data [OK]
Common Mistakes:
Thinking mongos stores data
Confusing mongos with config servers
Assuming mongos handles backups
2. Which of the following is the correct way for an application to connect in a sharded MongoDB setup?
easy
A. Connect to the primary replica set member.
B. Connect only to the config servers.
C. Connect directly to each shard individually.
D. Connect to the mongos router.
Solution
Step 1: Identify connection point in sharded cluster
Applications connect to mongos which routes queries to shards transparently.
Step 2: Exclude other options
Direct connection to shards or config servers is not recommended; primary replica set member is for replica sets, not sharding.
Final Answer:
Connect to the mongos router. -> Option D
Quick Check:
Apps connect to mongos [OK]
Hint: Always connect apps to mongos, not shards [OK]
Common Mistakes:
Connecting directly to shards
Using config servers for queries
Confusing replica sets with sharding
3. Given a sharded cluster with two shards and a mongos router, what will happen when you run this query through mongos?
db.users.find({ userId: 12345 })
Assume userId is the shard key.
medium
A. mongos returns an error because shard key is missing.
B. mongos sends the query to all shards and merges results.
C. mongos routes the query only to the shard containing userId 12345.
D. mongos sends the query to the config servers.
Solution
Step 1: Check query uses shard key
The query filters on userId, which is the shard key, so mongos can route precisely.
Step 2: Understand mongos routing behavior
mongos sends the query only to the shard that holds the chunk for userId 12345, not all shards.
Final Answer:
mongos routes the query only to the shard containing userId 12345. -> Option C
Quick Check:
Query with shard key routes to one shard [OK]
Hint: Query with shard key goes to one shard only [OK]
Common Mistakes:
Assuming mongos queries all shards always
Thinking config servers handle queries
Believing shard key is optional for routing
4. You wrote a query to find documents by userId in a sharded cluster but get slow results. You realize userId is not the shard key. What is the likely cause?
medium
A. mongos is routing the query to all shards (scatter-gather).
B. mongos is routing the query to only one shard.
C. mongos is ignoring the query and returning empty results.
D. mongos is connecting directly to config servers.
Solution
Step 1: Identify shard key usage in query
The query filters on userId, which is not the shard key, so mongos cannot route precisely.
Step 2: Understand mongos scatter-gather behavior
mongos sends the query to all shards (scatter-gather) causing slower results.
Final Answer:
mongos is routing the query to all shards (scatter-gather). -> Option A
Quick Check:
Query without shard key causes scatter-gather [OK]
Hint: Queries without shard key hit all shards, slowing response [OK]
Common Mistakes:
Assuming query routes to one shard without shard key
Thinking mongos returns empty results on missing shard key
Confusing config servers with query routers
5. In a sharded MongoDB cluster, you want to optimize query performance for a collection frequently queried by email. The current shard key is userId. What is the best approach involving mongos behavior?
hard
A. Change the shard key to email so mongos routes queries efficiently.
B. Add an index on email and keep the shard key as userId.
C. Connect directly to shards to bypass mongos routing.
D. Disable sharding on the collection to improve query speed.
Solution
Step 1: Analyze query pattern and shard key
Queries are frequent on email, but shard key is userId, causing scatter-gather queries via mongos.
Step 2: Optimize shard key for routing
Changing shard key to email lets mongos route queries directly to relevant shards, improving performance.
Final Answer:
Change the shard key to email so mongos routes queries efficiently. -> Option A
Quick Check:
Shard key matches query field for efficient mongos routing [OK]
Hint: Match shard key to frequent query field for fast mongos routing [OK]
Common Mistakes:
Thinking adding index alone fixes routing
Connecting directly to shards bypasses mongos is not recommended
Disabling sharding reduces scalability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.