Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
From the application’s perspective, there’s no difference between querying a sharded cluster and querying a single mongod. In both cases, the query interface and the process of iterating over the result set are the same. But behind the scenes, things are different, and it’s worthwhile to understand exactly what’s going on.
Imagine you’re querying a shard cluster. How many shards does mongos need to contact to return a proper query response? If you give it some thought, you’ll see that it depends on whether the shard key is present in the query selector. Remember that the config servers (and thus mongos) maintain a mapping of ranges to shards. These mappings are none other than the chunks that we examined earlier in the chapter. If a query includes the shard key, then mongos can quickly consult the chunk data to determine exactly which shard contains the query’s result set. This is called a targeted query.