A. Jesse Jiryu Davis put together a couple of scripts to explain how requests are routed in a MongoDB cluster:
In a sharded cluster of replica sets, which server or servers handle each of
your queries? What about each insert, update, or command? […]
Operations are routed according to the type of operation, your shard key,
and your read preference.
I actually think there are more hops involved between the config servers, mongos, and mongod instances. But the basic rules are pretty simple:
- if the query contains the shard key then it’s routed to specific shards
- if the query doesn’t contain shard keys then the request is send to all shards.
Original title and link: Understand MongoDB’s request routing ( ©myNoSQL)