skip to Main Content

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


  1. I think the documentation is quite clear:

    Read Operations

    MongoDB can route queries to a specific shard if the query includes at least the country field.

    For example, MongoDB can attempt a targeted read operation on the following query:

    chatDB = db.getSiblingDB("chat")
    chatDB.messages.find( { "country" : "UK" , "userid" : "123" } )
    

    Queries without the country field perform broadcast operations.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search