0
0
Firebasecloud~10 mins

Querying with where clause in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Querying with where clause
Start Query
Specify Collection
Add where clause
Filter documents
Return matching documents
End Query
This flow shows how a query starts by choosing a collection, then adds a where clause to filter documents, and finally returns only those that match.
Execution Sample
Firebase
const q = query(collection(db, "users"), where("age", ">", 18));
const querySnapshot = await getDocs(q);
querySnapshot.forEach((doc) => {
  console.log(doc.id, "=>", doc.data());
});
This code queries the 'users' collection for documents where the 'age' field is greater than 18, then prints each matching document.
Process Table
StepActionFilter ConditionDocuments CheckedDocuments MatchedOutput
1Start query on 'users' collectionage > 18All documents in 'users'N/ANo output yet
2Check document 1age=16Doc1NoSkip
3Check document 2age=22Doc2YesInclude Doc2
4Check document 3age=18Doc3NoSkip
5Check document 4age=25Doc4YesInclude Doc4
6End queryage > 18All checked2 matchedReturn Doc2, Doc4
💡 All documents checked; only those with age > 18 are returned.
Status Tracker
VariableStartAfter Doc1After Doc2After Doc3After Doc4Final
Documents Matched[][][Doc2][Doc2][Doc2, Doc4][Doc2, Doc4]
Key Moments - 2 Insights
Why does the document with age 18 not appear in the results?
Because the where clause uses '>' (greater than), not '>=' (greater or equal). The document with age 18 does not satisfy age > 18, as shown in execution_table row 4.
Are all documents checked even if some match early?
Yes, Firestore checks all documents in the collection to find all matches, as seen in execution_table rows 2 to 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, how many documents matched the where clause?
A2
B1
C3
D4
💡 Hint
Check the 'Documents Matched' column in the last row of the execution_table.
At which step does the query skip a document because it does not meet the condition?
AStep 3
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Documents Checked' and 'Documents Matched' columns in execution_table rows.
If the where clause changed to 'age >= 18', which document would be included additionally?
ADoc1
BDoc4
CDoc3
DNo additional document
💡 Hint
Check which document has age exactly 18 in the execution_table.
Concept Snapshot
Querying with where clause in Firebase:
- Use query() with collection() and where(field, operator, value)
- Filters documents by condition
- Returns only matching documents
- Operators: ==, >, >=, <, <=, etc.
- All documents checked, only matches returned
Full Transcript
This visual execution shows how a Firebase query with a where clause works. First, the query starts on the 'users' collection. Then, it applies the filter condition 'age > 18'. Each document in the collection is checked one by one. Documents with age 16 and 18 do not match and are skipped. Documents with age 22 and 25 match and are included in the results. Finally, the query returns only the matching documents. This helps you get only the data you want from your database.