Array-contains queries in Firebase - Commands & Configuration
firebase firestore:query --collection=users --where="skills array-contains 'javascript'"--collection - Specifies which collection to query.--where - Defines the condition for filtering documents.firebase firestore:query --collection=orders --where="items array-contains 'product789'"--collection - Specifies which collection to query.--where - Defines the condition for filtering documents.If you remember nothing else from this pattern, remember: array-contains queries let you find documents where a specific value exists inside an array field.
Practice
What does the array-contains query operator do in Firebase Firestore?
Solution
Step 1: Understand the purpose of
This operator is designed to check if an array field in a document contains a specific value.array-containsStep 2: Compare with other query types
Other options describe different query operators, but onlyarray-containsmatches arrays including a value.Final Answer:
Finds documents where an array field includes a specific value -> Option AQuick Check:
array-contains = finds value in array [OK]
- Confusing array-contains with substring search
- Using array-contains on non-array fields
- Thinking it works with multiple values at once
Which of the following is the correct syntax to query documents where the tags array contains the value "urgent"?
const query = firestore.collection('tasks').where(____);Solution
Step 1: Recall the correct operator for single value in array
The operator is "array-contains" to check if an array includes a specific value.Step 2: Check syntax format
The syntax is.where("field", "array-contains", value). "tags", "array-contains", "urgent" matches this exactly.Final Answer:
"tags", "array-contains", "urgent" -> Option DQuick Check:
Correct syntax uses array-contains [OK]
- Using == instead of array-contains for arrays
- Confusing array-contains with array-contains-any
- Using non-existent operator "contains"
Given the following documents in a Firestore collection projects:
[{ id: 1, members: ["alice", "bob"] }, { id: 2, members: ["carol"] }, { id: 3, members: ["bob", "dave"] }]What will the query firestore.collection('projects').where('members', 'array-contains', 'bob').get() return?
Solution
Step 1: Identify documents where 'members' array contains 'bob'
Document 1 has members ["alice", "bob"] which includes "bob". Document 3 has ["bob", "dave"] which also includes "bob". Document 2 does not.Step 2: Determine query result
The query returns documents 1 and 3 because their 'members' arrays contain "bob".Final Answer:
Documents with id 1 and 3 -> Option CQuick Check:
array-contains 'bob' = docs 1 and 3 [OK]
- Including documents without the value
- Confusing array-contains with array-contains-any
- Assuming partial matches inside strings
What is wrong with this Firestore query?
firestore.collection('orders').where('items', 'array-contains', ['apple', 'banana'])The items field is an array of strings.
Solution
Step 1: Understand array-contains value type
The 'array-contains' operator expects a single value, not an array, to check inside the array field.Step 2: Identify correct usage
Passing an array as the value causes an error. To check multiple values, 'array-contains-any' is used instead.Final Answer:
You cannot use an array as the value with array-contains -> Option AQuick Check:
array-contains needs single value, not array [OK]
- Passing an array as the value to array-contains
- Confusing array-contains with array-contains-any
- Assuming array-contains works like 'in' operator
You want to find all documents in the events collection where the attendees array contains both "john" and "jane". Which approach is correct?
Solution
Step 1: Understand limitations of array-contains and array-contains-any
array-contains checks for one value in array; array-contains-any checks if array contains any of multiple values but not all.Step 2: Find correct method to get documents containing both values
Since Firestore does not support querying for multiple values all present in one array in a single query, you must run two queries with array-contains and combine results client-side.Final Answer:
Use two queries with array-contains for each name and merge results client-side -> Option BQuick Check:
Multiple array-contains queries combined client-side [OK]
- Using array-contains-any expecting all values matched
- Passing array of values to array-contains
- Using equality check on arrays
Ready to prove it?
Pass the test and this pattern moves unknown
