Bird
Raised Fist0
Firebasecloud~20 mins

Querying and filtering in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Filtering documents with multiple conditions

You have a Firestore collection named users with documents containing fields age (number) and status (string). Which query will return all users older than 25 with status exactly 'active'?

Firebase
const query = firestore.collection('users').where('age', '>', 25).where('status', '==', 'active');
Afirestore.collection('users').where('age', '>=', 25).where('status', '!=', 'active')
Bfirestore.collection('users').where('age', '>', 25).where('status', '==', 'active')
Cfirestore.collection('users').where('age', '<', 25).where('status', '==', 'active')
Dfirestore.collection('users').where('age', '>', 25).where('status', 'array-contains', 'active')
Attempts:
2 left
💡 Hint

Remember that where filters must match the exact condition you want. The array-contains operator is for arrays, not strings.

❓ Architecture
intermediate
2:00remaining
Choosing the right index for compound queries

You want to run a Firestore query filtering by category and ordering by createdAt. What must you do to ensure the query runs efficiently?

ACreate a composite index on <code>category</code> ascending and <code>createdAt</code> descending
BCreate a composite index on <code>createdAt</code> ascending and <code>category</code> ascending
CNo index needed, Firestore handles all queries automatically
DCreate a single-field index on <code>createdAt</code> only
Attempts:
2 left
💡 Hint

Compound queries with filters and orderings require composite indexes matching the filter and order fields.

❓ security
advanced
2:00remaining
Securing filtered queries with Firestore rules

You want to allow users to read only documents from the orders collection where the userId field matches their own user ID. Which Firestore security rule enforces this?

Firebase
match /orders/{orderId} {
  allow read: if resource.data.userId == request.auth.uid;
}
Aallow read: if resource.data.userId == request.auth.uid;
Ballow read: if resource.data.userId != request.auth.uid;
Callow read: if request.auth.uid != null;
Dallow read: if resource.data.userId == 'admin';
Attempts:
2 left
💡 Hint

Check that the document's userId matches the authenticated user's ID.

✅ Best Practice
advanced
2:00remaining
Optimizing queries to reduce Firestore costs

You have a query that fetches all documents from a large collection but only needs the name and email fields. How can you reduce data transfer costs?

AUse <code>limit(1)</code> to reduce reads
BFetch all fields and ignore unused ones in code
CUse <code>select('name', 'email')</code> to fetch only needed fields
DUse <code>orderBy('name')</code> to optimize reads
Attempts:
2 left
💡 Hint

Fetching only required fields reduces data transferred and cost.

🧠 Conceptual
expert
2:00remaining
Understanding Firestore query limitations

Which of the following Firestore queries will cause an error due to Firestore's query limitations?

firestore.collection('products')
  .where('price', '>', 10)
  .where('price', '<', 50)
  .where('category', '==', 'books')
AThis query causes an error because Firestore requires an index on 'price' and 'category'
BThis query causes an error because Firestore does not allow multiple range filters on the same field
CThis query causes an error because Firestore does not allow combining equality and range filters
DThis query runs without error and returns products priced between 10 and 50 in 'books' category
Attempts:
2 left
💡 Hint

Firestore allows multiple range filters on the same field but only one field with range filters per query.

Practice

(1/5)
1. What does the where method do in a Firestore query?
easy
A. Limits the number of documents returned
B. Sorts documents in ascending order
C. Filters documents based on a condition
D. Deletes documents matching a condition

Solution

  1. Step 1: Understand the purpose of where

    The where method is used to filter documents by specifying a condition on a field.
  2. Step 2: Compare with other query methods

    orderBy sorts documents, limit restricts count, and deletion is unrelated.
  3. Final Answer:

    Filters documents based on a condition -> Option C
  4. Quick Check:

    where = filter condition [OK]
Hint: Remember: where filters, orderBy sorts, limit restricts [OK]
Common Mistakes:
  • Confusing where with orderBy
  • Thinking where limits results
  • Assuming where deletes data
2. Which of the following is the correct syntax to filter documents where the field age is greater than 18 in Firestore?
easy
A. query.where('age', '>', 18)
B. query.where('age' > 18)
C. query.where('age', 'greater', 18)
D. query.where('age', '>=', '18')

Solution

  1. Step 1: Check Firestore where syntax

    The correct syntax uses three arguments: field name, operator as string, and value.
  2. Step 2: Validate each option

    query.where('age', '>', 18) matches correct syntax. query.where('age' > 18) misses quotes and arguments. query.where('age', 'greater', 18) uses invalid operator. query.where('age', '>=', '18') compares number to string.
  3. Final Answer:

    query.where('age', '>', 18) -> Option A
  4. Quick Check:

    where(field, operator, value) correct format [OK]
Hint: Use quotes around operator and field name [OK]
Common Mistakes:
  • Omitting quotes around operator
  • Using invalid operators like 'greater'
  • Comparing number to string
3. Given this Firestore query code:
const query = collection.where('score', '>=', 50).orderBy('score').limit(3);

What will this query return?
medium
A. Top 3 documents with score less than 50
B. 3 documents with the lowest scores 50 or above
C. 3 documents with the highest scores 50 or above
D. All documents with score exactly 50

Solution

  1. Step 1: Analyze the where condition

    The query filters documents with score greater than or equal to 50.
  2. Step 2: Understand orderBy and limit

    Ordering by score defaults to ascending order, so lowest scores come first. Limit(3) returns first 3 documents in that order.
  3. Final Answer:

    3 documents with the lowest scores 50 or above -> Option B
  4. Quick Check:

    where >= 50 + orderBy ascending + limit 3 = lowest 3 scores ≥ 50 [OK]
Hint: orderBy defaults ascending unless specified [OK]
Common Mistakes:
  • Assuming orderBy sorts descending by default
  • Ignoring the where filter
  • Thinking limit returns highest scores
4. You wrote this Firestore query:
collection.where('status', '==', 'active').orderBy('createdAt')

But it throws an error about missing index. What is the best fix?
medium
A. Add a composite index for status and createdAt
B. Remove the orderBy clause
C. Change where to filter
D. Use limit(1) to avoid indexing

Solution

  1. Step 1: Understand Firestore indexing rules

    Queries with multiple conditions on different fields require composite indexes.
  2. Step 2: Identify the error cause

    The query filters by status and orders by createdAt, needing a composite index.
  3. Final Answer:

    Add a composite index for status and createdAt -> Option A
  4. Quick Check:

    Composite index needed for where + orderBy [OK]
Hint: Create composite index for multiple fields in query [OK]
Common Mistakes:
  • Removing orderBy instead of indexing
  • Using invalid method 'filter'
  • Thinking limit avoids index errors
5. You want to find users who are either under 18 or have a premium subscription. Which Firestore query approach is correct?
hard
A. Use a single query with where('age', '<', 18).orWhere('subscription', '==', 'premium')
B. Use orderBy on age and subscription fields
C. Use whereIn with both conditions combined
D. Use two separate queries with where and combine results in code

Solution

  1. Step 1: Understand Firestore query limitations

    Firestore does not support OR queries directly in a single query.
  2. Step 2: Choose correct workaround

    Run two queries separately: one for age < 18, another for subscription == premium, then merge results in code.
  3. Final Answer:

    Use two separate queries with where and combine results in code -> Option D
  4. Quick Check:

    Firestore lacks OR; combine queries manually [OK]
Hint: Firestore OR queries need multiple queries combined [OK]
Common Mistakes:
  • Trying to use non-existent orWhere method
  • Using whereIn incorrectly for OR logic
  • Assuming orderBy filters data
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