M103 - Chapter 3 - targeted vs. scatter gather queries - Last Quiz

Hi:
The documentation states the following:
“All sharded collections must have an index that supports the shard key. The index can be an index on the shard key or a compound index where the shard key is a prefix of the index.”
This implies that the shard key has to be a prefix of the compounded index.
Compound index is { “sku” : 1, “name” : 1 }.
The quiz lists 4 options from which to choose targeted queries, one of which should NOT be considered targeted:
db.products.find( { “name” : “MongoHacker”, “sku” : 1337 } )
Index order matter: ‘name’ is not a prefix to { “sku” : 1, “name” : 1 }; hence, such query is not targeted.
Please advise.
Thanks.

is exactly the same query as

{ “sku” : 1337 , “name” : “MongoHacker” }

Per documentation, ‘name’ predicate is NOT a prefix of the compound index: ‘sku’ is a prefix, but ‘name’ is NOT.
Please help me understand .
Thanks.

Remember in a query there is an implicit AND when fields are together. See the NOTE in https://docs.mongodb.com/manual/reference/operator/query/and/. So we have

which is equivalent to {$and:[{“name” : “MongoHacker”}, {“sku” : 1337}]}. If we let name:MongoHacker be A and B be sku:1337 we have A and B and since logical AND is commutative we and A and B is equivalent to B and A. So we have {$and:[{“name” : “MongoHacker”}, {“sku” : 1337}]} equivalent to {$and:[ {“sku” : 1337},{“name” : “MongoHacker”}]}. Being equivalent queries they both can use the same index.

For example:

// Starting collection
mongosh> c.find()
{ _id: 1, name: 'MongoHacker', sku: 1337 }
{ _id: 2, sku: 1337, name: 'MongoHacker' }
{ _id: 3, name: 'steevej', sku: 1337 }
{ _id: 4, name: 'MongoHacker', sku: 7331 }
// let's find name and sku
mongosh> c.find( { name: 'MongoHacker', sku: 1337 } )
{ _id: 1, name: 'MongoHacker', sku: 1337 }
{ _id: 2, sku: 1337, name: 'MongoHacker' }
// and then let's find sku and name
mongosh> c.find( {  sku: 1337, name: 'MongoHacker' } )
{ _id: 1, name: 'MongoHacker', sku: 1337 }
{ _id: 2, sku: 1337, name: 'MongoHacker' }

Hi.
I am aware of all that you added. But from an Index perspective, this query is NOT a targeted one because the leading field should be an index prefix, and ‘name’ is not. That’s what the documentation explicitly states:
All sharded collections must have an index that supports the shard key. The index can be an index on the shard key or a compound index where the shard key is a PREFIX of the INDEX.”

From an index perspective, this query IS a targeted one because the query {name:NameValue,sku:SkuValue} IS EQUIVALENT TO {sku:SkuValue,name:NameValue} and the same index CAN and WILL be used.

The order of the fields DOES NOT matter.