0
0
Firebasecloud~10 mins

Compound queries (multiple where) in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Compound queries (multiple where)
Start Query
Add first where condition
Add second where condition
Combine conditions
Send query to Firestore
Firestore filters documents
Return filtered results
End
The query starts by adding multiple where conditions, combines them, sends to Firestore, which filters documents and returns matching results.
Execution Sample
Firebase
db.collection('users')
  .where('age', '>=', 18)
  .where('status', '==', 'active')
  .get()
This query fetches users who are at least 18 years old and have an active status.
Process Table
StepActionCondition AddedQuery StateResulting Filter
1Start query on 'users' collectionnulldb.collection('users')No filter
2Add where conditionage >= 18db.collection('users').where('age', '>=', 18)Filter: age >= 18
3Add second where conditionstatus == 'active'db.collection('users').where('age', '>=', 18).where('status', '==', 'active')Filter: age >= 18 AND status == 'active'
4Send query to Firestoreage >= 18 AND status == 'active'Query sentFirestore applies both filters
5Firestore returns documentsage >= 18 AND status == 'active'Documents matching both conditionsFiltered results returned
6EndQuery completeResults receivedQuery finished
💡 Query ends after Firestore returns documents matching all where conditions.
Status Tracker
VariableStartAfter Step 2After Step 3Final
querydb.collection('users')db.collection('users').where('age', '>=', 18)db.collection('users').where('age', '>=', 18).where('status', '==', 'active')Query with both filters ready to execute
Key Moments - 2 Insights
Why do we chain multiple where() calls instead of combining conditions in one?
Firestore requires each where() call to add one condition; chaining combines them logically with AND. See execution_table rows 2 and 3 where each condition is added step-by-step.
What happens if one condition is not met by any document?
Firestore returns an empty result because all conditions must be true. This is shown in execution_table row 5 where only documents matching both filters are returned.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the query state?
Adb.collection('users').where('age', '>=', 18).where('status', '==', 'active')
Bdb.collection('users').where('age', '<', 18)
Cdb.collection('users').where('status', '!=', 'active')
Ddb.collection('users')
💡 Hint
Check the 'Query State' column at step 3 in the execution_table.
At which step does Firestore apply both filters to the documents?
AStep 2
BStep 1
CStep 4
DStep 3
💡 Hint
Look for when the query is sent to Firestore and filters are applied in the execution_table.
If we remove the second where condition, how would the final query state change?
AIt would filter by status == 'active' only
BIt would only filter by age >= 18
CIt would have no filters
DIt would filter by age < 18
💡 Hint
Refer to variable_tracker and see how query changes after each where() call.
Concept Snapshot
Compound queries in Firestore use multiple where() calls chained together.
Each where() adds one filter condition.
All conditions combine with AND logic.
Query runs only after all where() calls are chained.
Firestore returns documents matching all conditions.
Full Transcript
This visual execution shows how to build a Firestore query with multiple where conditions. We start with a collection reference, add the first where condition filtering by age, then add a second where condition filtering by status. These conditions combine logically with AND. When the query is sent to Firestore, it applies both filters and returns only documents matching both. Variables track the query state as conditions are added. Key moments clarify why chaining is needed and what happens if no documents match. The quiz tests understanding of query state and filter application steps.