0
0
MongoDBquery~10 mins

Mongos router behavior in MongoDB - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mongos router behavior
Client sends query
Mongos receives query
Mongos checks shard key
Shard key present?
NoBroadcast query to all shards
Route query to specific shard
Collect results from shards
Merge and return results to client
Mongos receives queries, checks if shard key is present, routes to specific shard or broadcasts to all, then merges results and returns to client.
Execution Sample
MongoDB
db.collection.find({shardKey: 5, status: 'active'})
Query with shard key routes to one shard; results returned from that shard only.
Execution Table
StepActionShard Key Present?Routing DecisionShards QueriedResult Collected
1Client sends queryYesCheck shard keyN/AN/A
2Mongos receives queryYesRoute to shard with shardKey=5Shard 2N/A
3Shard 2 executes queryN/AN/AShard 2Documents matching query
4Mongos collects resultsN/AN/AShard 2Documents from Shard 2
5Mongos returns results to clientN/AN/AN/AFinal result set
💡 Query routed to shard based on shard key; results returned from that shard only.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
query{shardKey:5, status:'active'}{shardKey:5, status:'active'}{shardKey:5, status:'active'}{shardKey:5, status:'active'}
shardKeyPresentundefinedtruetruetrue
targetShardsundefinedShard 2Shard 2Shard 2
resultsemptyemptydocuments from Shard 2documents from Shard 2
Key Moments - 2 Insights
Why does Mongos route the query to only one shard when the shard key is present?
Because the shard key identifies the exact shard holding the data, Mongos routes the query directly to that shard (see execution_table step 2). This avoids querying all shards and improves efficiency.
What happens if the shard key is missing in the query?
Mongos broadcasts the query to all shards since it cannot determine which shard holds the data. This is less efficient but necessary to find matching documents.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the routing decision Mongos makes?
ARoute query to shard with shardKey=5
BBroadcast query to all shards
CReject the query
DCache the query for later
💡 Hint
Check the 'Routing Decision' column at step 2 in the execution_table.
At which step does Mongos collect results from shards?
AStep 3
BStep 4
CStep 1
DStep 5
💡 Hint
Look for the 'Collect results from shards' action in the execution_table.
If the query did not include the shard key, how would the 'Shards Queried' column change at step 2?
AIt would list only one shard
BIt would be empty
CIt would list all shards
DIt would list no shards
💡 Hint
Recall that without shard key, Mongos broadcasts query to all shards.
Concept Snapshot
Mongos router receives client queries.
Checks if shard key is present in query.
If yes, routes query to specific shard.
If no, broadcasts query to all shards.
Collects and merges results before returning to client.
Full Transcript
Mongos acts as a router in a MongoDB sharded cluster. When a client sends a query, Mongos first checks if the query contains the shard key. If the shard key is present, Mongos routes the query directly to the shard that holds the relevant data. This targeted routing improves efficiency by querying only one shard. If the shard key is missing, Mongos broadcasts the query to all shards to find matching documents. After shards execute the query, Mongos collects and merges the results, then returns the final result set to the client. This behavior ensures efficient and correct query routing in a sharded environment.