A gaming company stores player profiles in Azure Cosmos DB using the NoSQL API. Each profile is a JSON document containing fields like playerId, userName, level, inventory (an array of items), and friends (an array of playerIds). The application frequently needs to query all players that have a specific item in their inventory (e.g., 'sword'). Which Cosmos DB feature should they use to support this query efficiently?
Adding a wildcard index, such as /inventory/[]/? to the indexing policy, instructs Cosmos DB to index every element of the inventory array rather than only the first element, which is the default behavior. With that index in place, ARRAY_CONTAINS queries can use a targeted index seek instead of scanning every document, making the lookup both fast and cost-efficient in terms of request units. This is the correct configuration for a gaming company that frequently queries player profiles based on whether an array contains a specific item, because it directly aligns the index with the predicate.
Why this answer
A wildcard index in the indexing policy allows Azure Cosmos DB to automatically index all properties within a JSON document, including nested array elements like those in the 'inventory' array. This enables efficient queries such as 'SELECT * FROM c WHERE ARRAY_CONTAINS(c.inventory, {name: "sword"})' without requiring a custom composite index for each possible item. Without a wildcard index, the query would require a full scan of all documents, which is inefficient at scale.
Exam trap
The trap here is that candidates often confuse indexing features, thinking a composite index (Option C) is needed for array queries, when in fact composite indexes are for multi-property equality or range filters, not for array membership queries which require a wildcard index to index the array elements themselves.
How to eliminate wrong answers
Option A is wrong because the change feed is a mechanism for capturing document inserts, updates, and deletes in chronological order, not for querying current data based on array contents. Option B is wrong because stored procedures are server-side JavaScript logic for transactional operations, not a query optimization feature for indexing array elements. Option C is wrong because a composite index is designed to optimize queries with multiple filter conditions (e.g., WHERE level = 10 AND userName = 'Alice'), not for queries that filter on array membership like 'inventory contains item X'.