user734796
user734796

Reputation: 1

Sharding in MongoDB by location but don't query with it

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?

Upvotes: 0

Views: 75

Answers (2)

Joe
Joe

Reputation: 28366

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.

Upvotes: 0

Wernfried Domscheit
Wernfried Domscheit

Reputation: 59642

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.

Upvotes: 1

Related Questions