Bird
Raised Fist0
Firebasecloud~10 mins

Array-contains queries in Firebase - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to query documents where the 'tags' array contains 'cloud'.

Firebase
const query = firestore.collection('articles').where('tags', '[1]', 'cloud');
Drag options to blanks, or click blank then click option'
Aarray-contains
B==
Cin
Darray-contains-any
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'array-contains' for array fields.
Using 'in' which checks for values in a list, not inside arrays.
2fill in blank
medium

Complete the code to query documents where the 'categories' array contains any of the values in ['tech', 'science'].

Firebase
const query = firestore.collection('posts').where('categories', '[1]', ['tech', 'science']);
Drag options to blanks, or click blank then click option'
Aarray-contains-any
Bin
Carray-contains
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array-contains' which only checks for one value.
Using 'in' which checks if a field equals any value in a list, not array contents.
3fill in blank
hard

Fix the error in the query to find documents where 'skills' array contains 'firebase'.

Firebase
const query = firestore.collection('users').where('skills', '[1]', 'firebase');
Drag options to blanks, or click blank then click option'
A==
Barray-contains-any
Cin
Darray-contains
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' which does not work for array fields.
Using 'in' which is for matching field values, not array contents.
4fill in blank
hard

Fill both blanks to query documents where the 'features' array contains any of the values ['fast', 'reliable'], and limit to 5 results.

Firebase
const query = firestore.collection('products').where('features', '[1]', ['fast', 'reliable']).limit([2]);
Drag options to blanks, or click blank then click option'
Aarray-contains-any
Barray-contains
C5
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array-contains' which only accepts a single value, not an array.
Not setting a limit or using an invalid limit value.
5fill in blank
hard

Fill all three blanks to query documents where 'tags' array contains 'cloud', order by 'date', and limit to 3 results.

Firebase
const query = firestore.collection('events').where('tags', '[1]', 'cloud').orderBy('[2]').limit([3]);
Drag options to blanks, or click blank then click option'
Aarray-contains
Bdate
C3
Dtimestamp
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array-contains-any' when only one value is needed.
Ordering by a wrong field name.
Setting an invalid limit number.

Practice

(1/5)
1.

What does the array-contains query operator do in Firebase Firestore?

easy
A. Finds documents where an array field includes a specific value
B. Finds documents where a string field contains a substring
C. Finds documents where a number field is greater than a value
D. Finds documents where a field is missing

Solution

  1. Step 1: Understand the purpose of array-contains

    This operator is designed to check if an array field in a document contains a specific value.
  2. Step 2: Compare with other query types

    Other options describe different query operators, but only array-contains matches arrays including a value.
  3. Final Answer:

    Finds documents where an array field includes a specific value -> Option A
  4. Quick Check:

    array-contains = finds value in array [OK]
Hint: Remember: array-contains checks for one value inside an array [OK]
Common Mistakes:
  • Confusing array-contains with substring search
  • Using array-contains on non-array fields
  • Thinking it works with multiple values at once
2.

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(____);
easy
A. "tags", "contains", "urgent"
B. "tags", "==", "urgent"
C. "tags", "array-contains-any", ["urgent"]
D. "tags", "array-contains", "urgent"

Solution

  1. 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.
  2. Step 2: Check syntax format

    The syntax is .where("field", "array-contains", value). "tags", "array-contains", "urgent" matches this exactly.
  3. Final Answer:

    "tags", "array-contains", "urgent" -> Option D
  4. Quick Check:

    Correct syntax uses array-contains [OK]
Hint: Use "array-contains" for single value array queries [OK]
Common Mistakes:
  • Using == instead of array-contains for arrays
  • Confusing array-contains with array-contains-any
  • Using non-existent operator "contains"
3.

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?

medium
A. Documents with id 2 only
B. Documents with id 1 only
C. Documents with id 1 and 3
D. No documents

Solution

  1. 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.
  2. Step 2: Determine query result

    The query returns documents 1 and 3 because their 'members' arrays contain "bob".
  3. Final Answer:

    Documents with id 1 and 3 -> Option C
  4. Quick Check:

    array-contains 'bob' = docs 1 and 3 [OK]
Hint: Check which arrays include the value exactly [OK]
Common Mistakes:
  • Including documents without the value
  • Confusing array-contains with array-contains-any
  • Assuming partial matches inside strings
4.

What is wrong with this Firestore query?

firestore.collection('orders').where('items', 'array-contains', ['apple', 'banana'])

The items field is an array of strings.

medium
A. You cannot use an array as the value with array-contains
B. The operator should be 'array-contains-any' instead
C. The field 'items' must be a string, not an array
D. The query syntax is correct

Solution

  1. Step 1: Understand array-contains value type

    The 'array-contains' operator expects a single value, not an array, to check inside the array field.
  2. Step 2: Identify correct usage

    Passing an array as the value causes an error. To check multiple values, 'array-contains-any' is used instead.
  3. Final Answer:

    You cannot use an array as the value with array-contains -> Option A
  4. Quick Check:

    array-contains needs single value, not array [OK]
Hint: array-contains accepts only one value, not arrays [OK]
Common Mistakes:
  • Passing an array as the value to array-contains
  • Confusing array-contains with array-contains-any
  • Assuming array-contains works like 'in' operator
5.

You want to find all documents in the events collection where the attendees array contains both "john" and "jane". Which approach is correct?

hard
A. Use a single query with array-contains-any and pass both names
B. Use two queries with array-contains for each name and merge results client-side
C. Use a single query with array-contains and pass an array of both names
D. Use a query with where('attendees', '==', ['john', 'jane'])

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Use two queries with array-contains for each name and merge results client-side -> Option B
  4. Quick Check:

    Multiple array-contains queries combined client-side [OK]
Hint: Firestore can't query multiple array-contains in one query [OK]
Common Mistakes:
  • Using array-contains-any expecting all values matched
  • Passing array of values to array-contains
  • Using equality check on arrays
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard