0
0
Firebasecloud~10 mins

In and not-in queries in Firebase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - In and not-in queries
Start Query
Check Query Type
in query
Match docs
Return docs
End
The query starts by checking if it is an 'in' or 'not-in' query, then matches or excludes documents accordingly, and finally returns the results.
Execution Sample
Firebase
db.collection('users')
  .where('status', 'in', ['active', 'pending'])
  .get()
  .then(snapshot => {
    // process docs
  })
This code queries the 'users' collection for documents where the 'status' field is either 'active' or 'pending'.
Process Table
StepQuery TypeFieldOperatorValuesActionResult
1instatusin['active', 'pending']Check each doc's statusMatch docs with status 'active' or 'pending'
2instatusin['active', 'pending']Collect matched docsDocs with status 'active' or 'pending' collected
3not-instatusnot-in['inactive', 'banned']Check each doc's statusExclude docs with status 'inactive' or 'banned'
4not-instatusnot-in['inactive', 'banned']Collect remaining docsDocs without status 'inactive' or 'banned' collected
5end---Return final docsQuery complete, results returned
💡 Query ends after collecting matching or excluding documents based on 'in' or 'not-in' operator.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
matchedDocs[]['doc1', 'doc3']['doc1', 'doc3']--['doc1', 'doc3']
excludedDocs[]--['doc2', 'doc4']['doc2', 'doc4']['doc2', 'doc4']
finalDocs[]----['doc1', 'doc3'] or ['doc1', 'doc3', 'doc5'] depending on query
Key Moments - 3 Insights
Why does the 'in' query only return documents with specific values?
Because in step 1 and 2 of the execution_table, the query checks each document's field and only collects those matching the listed values.
How does the 'not-in' query exclude documents?
As shown in steps 3 and 4, the query checks each document and excludes those with values in the 'not-in' list, collecting only the rest.
Can 'in' and 'not-in' queries be combined in one query?
No, Firebase queries allow only one 'in' or 'not-in' filter per query, so you must run separate queries or restructure your data.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what documents are collected after step 2 in an 'in' query?
ADocuments with status 'active' or 'pending'
BDocuments with status 'inactive' or 'banned'
CAll documents in the collection
DNo documents
💡 Hint
Refer to row 2 in execution_table where matched docs are collected for 'in' query.
At which step does the 'not-in' query exclude documents with certain statuses?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Check step 3 in execution_table where exclusion happens for 'not-in' query.
If you add 'inactive' to the 'in' query values, how does the matchedDocs variable change after step 2?
AIt excludes documents with status 'inactive'
BIt remains the same
CIt includes documents with status 'inactive' as well
DIt becomes empty
💡 Hint
Look at variable_tracker matchedDocs after step 2 and consider adding 'inactive' to the 'in' values.
Concept Snapshot
Firebase 'in' and 'not-in' queries filter documents by matching or excluding specific field values.
Use 'in' to get docs with any value in a list.
Use 'not-in' to exclude docs with values in a list.
Only one 'in' or 'not-in' filter per query is allowed.
Results are returned after filtering documents accordingly.
Full Transcript
This visual execution shows how Firebase 'in' and 'not-in' queries work step-by-step. The query starts by checking if it is an 'in' or 'not-in' query. For 'in' queries, it checks each document's field and collects those matching any value in the list. For 'not-in' queries, it excludes documents with values in the list and collects the rest. Variables track matched and excluded documents after each step. The query ends by returning the filtered documents. Key points include understanding how documents are matched or excluded and that only one 'in' or 'not-in' filter can be used per query. The quiz tests understanding of which documents are collected or excluded at specific steps and how changing values affects results.