Bird
Raised Fist0
Firebasecloud~20 mins

Querying with where clause in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Where Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Filtering documents with a single where clause
Given a Firestore collection users with documents containing fields age and city, what will be the output of this query?

db.collection('users').where('age', '>', 25).get()

Assuming the collection has these documents:
- {name: 'Alice', age: 30, city: 'NY'}
- {name: 'Bob', age: 22, city: 'LA'}
- {name: 'Carol', age: 27, city: 'NY'}
- {name: 'Dave', age: 24, city: 'SF'}
Firebase
db.collection('users').where('age', '>', 25).get()
A[{name: 'Alice', age: 30, city: 'NY'}, {name: 'Carol', age: 27, city: 'NY'}]
B[{name: 'Bob', age: 22, city: 'LA'}, {name: 'Dave', age: 24, city: 'SF'}]
C[{name: 'Alice', age: 30, city: 'NY'}, {name: 'Bob', age: 22, city: 'LA'}]
D[]
Attempts:
2 left
💡 Hint
Think about which users have age greater than 25.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in a Firestore where clause
Which option contains a syntax error in the Firestore query using where clause?
Firebase
db.collection('products').where('price' '>=', 100).get()
Adb.collection('products').where('price', '>=', 100).get()
Bdb.collection('products').where('price', '>=', '100').get()
Cdb.collection('products').where('price', '=>', 100).get()
Ddb.collection('products').where('price' '>=', 100).get()
Attempts:
2 left
💡 Hint
Check the commas and operator spelling in the where clause.
❓ optimization
advanced
2:30remaining
Optimizing multiple where clauses in Firestore
You want to query a collection orders to find documents where status is 'shipped' and total is greater than 50. Which query is optimized and valid in Firestore?
Adb.collection('orders').where('status', '==', 'shipped' || 'total', '>', 50).get()
Bdb.collection('orders').where('status', '==', 'shipped').where('total', '>', 50).get()
Cdb.collection('orders').where('status' == 'shipped' && 'total' > 50).get()
Ddb.collection('orders').where('status', '==', 'shipped' && 'total', '>', 50).get()
Attempts:
2 left
💡 Hint
Firestore supports chaining multiple where clauses separately.
🧠 Conceptual
advanced
2:00remaining
Understanding Firestore where clause limitations
Which statement about Firestore where clause limitations is true?
AYou cannot combine <code>where</code> clauses with <code>orderBy</code> on different fields without an index.
BYou can use multiple inequality filters on different fields in a single query.
CFirestore allows <code>where</code> clauses with OR logic directly.
DYou can use <code>where</code> clauses with array fields using the '!=' operator.
Attempts:
2 left
💡 Hint
Think about Firestore indexing and query constraints.
🔧 Debug
expert
3:00remaining
Debugging a Firestore query with multiple where clauses
You run this query:
db.collection('employees').where('department', '==', 'sales').where('age', '<', 30).where('age', '>', 40).get()

What will happen when you run it?
AThe query throws an error because multiple inequality filters on the same field with conflicting conditions are not allowed.
BThe query returns employees in sales department with age between 30 and 40.
CThe query returns employees in sales department with age less than 30 and greater than 40.
DThe query returns all employees in sales department ignoring age filters.
Attempts:
2 left
💡 Hint
Check Firestore rules about multiple inequality filters on the same field.

Practice

(1/5)
1. What does the where clause do in a Firebase query?
easy
A. Filters documents based on a condition
B. Deletes documents from a collection
C. Adds new documents to a collection
D. Sorts documents alphabetically

Solution

  1. Step 1: Understand the purpose of where clause

    The where clause is used to filter documents in a collection based on a condition.
  2. Step 2: Compare with other options

    Deleting, adding, or sorting documents are different operations and not done by where.
  3. Final Answer:

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

    where clause = filter condition [OK]
Hint: Remember: where filters, not adds or deletes [OK]
Common Mistakes:
  • Confusing filtering with deleting data
  • Thinking where adds new documents
  • Assuming where sorts data
2. Which of the following is the correct syntax to query documents where the field age is equal to 25 in Firebase?
easy
A. db.collection('users').filter('age', '==', 25)
B. db.collection('users').where('age' = 25)
C. db.collection('users').where('age', '=', 25)
D. db.collection('users').where('age', '==', 25)

Solution

  1. Step 1: Recall Firebase where clause syntax

    The correct syntax uses where(field, operator, value) with operator as a string, e.g., '=='.
  2. Step 2: Check each option

    db.collection('users').where('age', '==', 25) matches correct syntax. db.collection('users').where('age' = 25) uses '=' instead of '=='. db.collection('users').filter('age', '==', 25) uses 'filter' which is incorrect. db.collection('users').where('age', '=', 25) uses '=' instead of '=='.
  3. Final Answer:

    db.collection('users').where('age', '==', 25) -> Option D
  4. Quick Check:

    where(field, '==', value) syntax [OK]
Hint: Use '==' operator as a string in where clause [OK]
Common Mistakes:
  • Using '=' instead of '==' in where clause
  • Using filter() instead of where()
  • Missing quotes around operator
3. Given the following Firebase query:
db.collection('products').where('price', '>=', 100).get()

Which products will be returned?
medium
A. Products with price less than 100
B. Products with price equal to 100 only
C. Products with price greater than or equal to 100
D. All products regardless of price

Solution

  1. Step 1: Understand the operator '>=' in where clause

    The operator '>=' means greater than or equal to the given value.
  2. Step 2: Apply the condition to the query

    The query returns products where price is 100 or more.
  3. Final Answer:

    Products with price greater than or equal to 100 -> Option C
  4. Quick Check:

    price >= 100 means price ≥ 100 [OK]
Hint: '>=' means greater or equal, so include 100 and above [OK]
Common Mistakes:
  • Thinking '>=' means only greater than
  • Confusing '<' with '>='
  • Assuming it returns all products
4. What is wrong with this Firebase query?
db.collection('orders').where('status', 'equals', 'shipped').get()
medium
A. The operator 'equals' is invalid in where clause
B. The collection name 'orders' is incorrect
C. The field 'status' cannot be queried
D. The get() method is missing parentheses

Solution

  1. Step 1: Check the operator used in where clause

    Firebase supports operators like '==', '<', '>=', but not 'equals'.
  2. Step 2: Verify other parts of the query

    Collection name and field are valid, and get() is correctly called with parentheses.
  3. Final Answer:

    The operator 'equals' is invalid in where clause -> Option A
  4. Quick Check:

    Use '==' not 'equals' in where clause [OK]
Hint: Use '==' operator, not words like 'equals' [OK]
Common Mistakes:
  • Using 'equals' instead of '=='
  • Missing parentheses on get()
  • Assuming any string operator works
5. You want to get all users from the 'users' collection whose age is between 18 and 30 inclusive. Which Firebase query correctly achieves this?
hard
A. db.collection('users').where('age', '>', 18).where('age', '<', 30)
B. db.collection('users').where('age', '>=', 18).where('age', '<=', 30)
C. db.collection('users').where('age', '==', 18).where('age', '==', 30)
D. db.collection('users').where('age', '>=', 18 || '<=', 30)

Solution

  1. Step 1: Understand how to filter a range in Firebase

    Firebase allows chaining multiple where clauses to filter a range, e.g., age >= 18 and age <= 30.
  2. Step 2: Analyze each option

    db.collection('users').where('age', '>=', 18).where('age', '<=', 30) correctly uses two where clauses with '>=' and '<=' operators. db.collection('users').where('age', '>', 18).where('age', '<', 30) excludes 18 and 30 by using '>' and '<'. db.collection('users').where('age', '==', 18).where('age', '==', 30) tries to match age exactly 18 and 30 simultaneously, which is impossible. db.collection('users').where('age', '>=', 18 || '<=', 30) uses invalid syntax combining operators in one where.
  3. Final Answer:

    db.collection('users').where('age', '>=', 18).where('age', '<=', 30) -> Option B
  4. Quick Check:

    Chain where with >= and <= for inclusive range [OK]
Hint: Chain where clauses for range with >= and <= [OK]
Common Mistakes:
  • Using '==' twice for range
  • Combining operators incorrectly in one where
  • Using > and < excludes boundary values
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