I want to segment data based on a geographic area by following this guide by MongoDB, to make an application globally accessible with low-latency. The data should be segmented by using the shard key { country : 1, userid : 1 }
, and different countries are assigned to a different shard. For example all documents from country = US, CA, MX
should be stored on a specific shard.
However, I am not sure if I can benefit from this improved latency, if my queries don’t include country
. For example: will MongoDB be able to do a targeted read, if my query only contains the userid
?
2
Answers
I think the documentation is quite clear:
If the query doesn’t include the shard key, the request will be sent to every shard, and the mongos will not send a response back to the client until it has received a response from every shard.
However, the response from any shard that does not contain any matching documents will be a very short, empty response instead of sending the entire document from a distant shard.
So while the latency will be worse than if the query did include the shard key, it should be better than if the data were actually stored in a distant shard.