0
0
Firebasecloud~20 mins

Array-contains queries in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array-contains Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2: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?

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?
AUsers A, B, and C
BUsers B and C only
CUser C only
DUsers A and C only
Attempts:
2 left
💡 Hint
The 'array-contains' operator matches documents where the array field contains the specified value anywhere.
🧠 Conceptual
intermediate
2:00remaining
Limitations of array-contains queries
Which of the following statements about Firestore's 'array-contains' queries is TRUE?
A'array-contains' can be used to check if an array contains multiple values at once.
B'array-contains' can be combined with 'array-contains-any' in the same query.
C'array-contains' can only check for a single value inside an array field.
D'array-contains' works on fields that are strings, not arrays.
Attempts:
2 left
💡 Hint
Think about what 'array-contains' means: it checks for one value inside an array.
Configuration
advanced
2: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?
Adb.collection('products').where('tags', 'array-contains-any', ['eco', 'sale']).get()
Bdb.collection('products').where('tags', 'array-contains', ['eco', 'sale']).get()
Cdb.collection('products').where('tags', 'in', ['eco', 'sale']).get()
Ddb.collection('products').where('tags', 'array-contains-all', ['eco', 'sale']).get()
Attempts:
2 left
💡 Hint
'array-contains-any' checks if the array contains any of the listed values.
security
advanced
2: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?
Aallow read: if 'admin' in resource.data.roles;
Ballow read: if resource.data.roles == 'admin';
Callow read: if request.auth.token.roles.array-contains('admin');
Dallow read: if resource.data.roles.contains('admin');
Attempts:
2 left
💡 Hint
Firestore security rules use JavaScript-like syntax with 'in' to check array membership.
Architecture
expert
2: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?
ACreate a composite index on 'items' and 'orderDate' fields to support 'array-contains' queries.
BNo index is needed; Firestore automatically indexes array fields for 'array-contains' queries.
CManually create a single-field index on 'items' to enable 'array-contains' queries.
DDisable indexing on 'items' to improve write performance since 'array-contains' queries do not use indexes.
Attempts:
2 left
💡 Hint
Firestore automatically indexes array fields for 'array-contains' queries without extra setup.