Targeted Queries with more than one Shard Key

Hi everybody,

I have a question concerning one of the questions during M103.

Given that I have a shard key like this:

{ "country": 1, "_id": 1 }

How come that this query is considered a targeted query?

db.customers.find({"country": { $gte: "Portugal", $lte: "Spain" }})

In my understanding, the query must contain both shard keys in the query in order to be considered a targeted query. But the given query contains the country only. Can somebody explain why this is considered targeted?

Thanks in advance!

Hi @Marco_80669,
This query is targeted because it’s using a prefix of the shard key. The concept is very similar to the concept of index prefixes.

In case you have any doubts, please feel free to reach out to us.

Thanks and Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

1 Like

Hi @SourabhBagrecha

thank you for your reply! The documentation helped understanding targeted queries.


1 Like