I'm trying to understand how the shards will increment indexStats based on a question in MongoDB University. I got the answer, but still struggling to understand it.
Here's the question:
You have a sharded cluster with 4 shards. The products collection is sharded on { name : 1, dateCreated : 1 }. There are 4 chunks, one on each server, and here is the range of the shard key:
s1: { name: MinKey, dateCreated: MinKey} - { name: "e", dateCreated: MinKey}
s2: { name: "e", dateCreated: MinKey} - { name: "g", dateCreated: MinKey}
s3: { name: "g", dateCreated: MinKey} - { name: "m", dateCreated: MinKey}
s4:{ name: "m", dateCreated: MinKey} - { name: MaxKey, dateCreated: MaxKey}
You have the following indexes:
{ _id: 1 }
{ name: 1, dateCreated: 1 }
{ price : 1 }
{ category: 1 }
You perform the following query:
db.products.find( { name : { $in : [ "iphone", "ipad", "apple watch" ] } } )
Which of the shards will increment their value of $indexStats for an index as a result of this query?
Answer is: s1 and s3
The first thing to note is that the query uses the shard key, and that it uses just the index prefix. Next, we note that:
"iPhone" is in shard s3
"iPad" is in shard s3
"Apple Watch" is in shard s1
Therefore, it'll be a targeted query, and will hit shards s1 and s3.
While it wasn't part of the question, it increments the {name : 1, dateCreated : 1} index on those shards.
So how do you find that iPhone and iPad go to s3
shard but not to the s2
? If anyone could briefly explain this to me, I'd truly appreciate it.
Thanks!
Shard key ranges are cached in the mongoses from the config server , when you query the cluster from mongoses using the shard key the mongoses already know to which shard to forward the query so only in those shards the $indexStats value for {name:1,dateCreated:1} will increment.