Reputation: 2761
I am new to mongodb sharding. We know that we shard a database for scaling purpose. Where we need to use a sharding key so that the database distributing system can distribute data based on those key values. My question is - will I have to attach the sharding key with the query to avoid unnecessary query on other sharded db servers or does this work for mongodb sharding ?
Upvotes: 1
Views: 435
Reputation: 10707
When your search is having the shard key index in the query , the mongodb sharded cluster routing service(mongos) will forward your query directly to the shard for which the query is addressed avoiding the "scatter-gather" request to all shards. This is because the mongos is caching the shard key index ranges and know in advance to which shard to address the request.
Moreover in specific write cases in mongodb version >=4.2 including the shard key in the query part is compulsory.
Also if your data is distributed between few shards sending broadcast requests is not an issue , but in case you have 100x shards you will need to wait for all 100 shards to search and reply to the mongos , so in general not using the shard key is a not scalable operation and need to be avoided whenever possible.
I hope this answer your question ...
Upvotes: 1