Indexing arrays creates multikey entries. Array semantics and index restrictions differ from scalar fields.
Before you start
You should understand documents, collections and indexes. Sketch representative documents and the reads and writes they must support. MongoDB-specific examples assume a collection with the shown fields; deployment topology, permissions and existing indexes can affect the operational behavior being discussed.
The practical goal is to reason through this situation: A tags index supports finding documents containing a tag. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Inspect array structure
Index entries can correspond to multiple elements in one document.
Step 2: Relate predicates deliberately
Separate conditions may be satisfied by different elements.
Step 3: Use same-element matching
Apply $elemMatch when one array element must satisfy both conditions.
Worked scenario
A tags index supports finding documents containing a tag.
db.orders.find({ items: { $elemMatch: { sku: 'p1', quantity: { $gte: 2 } } } });This requires one item with SKU p1 and quantity at least two. Independent dotted conditions could match SKU in one item and quantity in another, changing the business result.
Common mistake
Conditions on multiple array properties may match different elements unless explicitly related.
Verify the behavior
Seed split-match and same-element documents, then compare results and multikey plans.
Interview exercise
Require two conditions on one element.
Answer and reasoning
Use an appropriate elemMatch shape and verify both query semantics and index behavior.
Continue learning
Compare the scenario with the MongoDB interview questions and test your understanding with the MongoDB MCQs. For terminology and implementation details, consult the reference material.