Challenge - 5 Problems
Array-contains Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate2:00remaining
Understanding array-contains query behavior
Given a Firestore collection 'users' where each document has a field 'roles' which is an array of strings, what will the following query return?
Assuming documents:
- User A: roles = ['admin', 'editor']
- User B: roles = ['viewer']
- User C: roles = ['admin']
Which users will be returned by this query?
db.collection('users').where('roles', 'array-contains', 'admin').get()Assuming documents:
- User A: roles = ['admin', 'editor']
- User B: roles = ['viewer']
- User C: roles = ['admin']
Which users will be returned by this query?
Attempts:
2 left
💡 Hint
The 'array-contains' operator matches documents where the array field contains the specified value anywhere.
✗ Incorrect
The query filters documents where the 'roles' array contains the string 'admin'. User A has 'admin' in roles, User C has 'admin', but User B does not. So only Users A and C are returned.
🧠 Conceptual
intermediate2:00remaining
Limitations of array-contains queries
Which of the following statements about Firestore's 'array-contains' queries is TRUE?
Attempts:
2 left
💡 Hint
Think about what 'array-contains' means: it checks for one value inside an array.
✗ Incorrect
'array-contains' only checks if a single value exists inside an array field. To check multiple values, 'array-contains-any' is used. It cannot be combined with 'array-contains' in the same query.
❓ Configuration
advanced2:00remaining
Correct Firestore query for multiple array values
You want to find documents in a 'products' collection where the 'tags' array contains either 'eco' or 'sale'. Which Firestore query code correctly achieves this?
Attempts:
2 left
💡 Hint
'array-contains-any' checks if the array contains any of the listed values.
✗ Incorrect
'array-contains-any' allows querying documents where the array field contains at least one of the specified values. 'array-contains' only accepts a single value, 'in' works on field values, not array contents, and 'array-contains-all' does not exist.
❓ security
advanced2:00remaining
Security rules impact on array-contains queries
Consider Firestore security rules that restrict read access to documents where the 'roles' array contains 'admin'. Which rule snippet correctly enforces this for read operations?
Attempts:
2 left
💡 Hint
Firestore security rules use JavaScript-like syntax with 'in' to check array membership.
✗ Incorrect
In Firestore security rules, to check if an array contains a value, use the 'in' operator: 'value in array'. Option A correctly uses 'admin' in resource.data.roles. Options A and D use invalid syntax, and B compares array to string.
❓ Architecture
expert2:00remaining
Designing Firestore indexes for array-contains queries
You have a Firestore collection 'orders' with a field 'items' which is an array of product IDs. You want to efficiently query orders containing a specific product ID using 'array-contains'. What is the best practice regarding indexes to support this query?
Attempts:
2 left
💡 Hint
Firestore automatically indexes array fields for 'array-contains' queries without extra setup.
✗ Incorrect
Firestore automatically creates single-field indexes on array fields to support 'array-contains' queries. Composite indexes are needed only when combining multiple where clauses or ordering. Disabling indexing on 'items' would break queries.