Bird
Raised Fist0
Firebasecloud~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1.

What does the in query do in Firebase Firestore?

easy
A. Finds documents where a field is null
B. Finds documents where a field does not exist
C. Finds documents where a field matches exactly one value only
D. Finds documents where a field matches any value in a given list

Solution

  1. Step 1: Understand the purpose of in query

    The in query checks if a field's value matches any value from a list of values.
  2. Step 2: Compare with other options

    Null fields and non-existent fields describe different query types, while limiting to exactly one value is incorrect for in.
  3. Final Answer:

    Finds documents where a field matches any value in a given list -> Option D
  4. Quick Check:

    in query = matches any value in list [OK]
Hint: Remember: 'in' means match any value from a list [OK]
Common Mistakes:
  • Confusing 'in' with 'not-in' query
  • Thinking 'in' matches only one value
  • Assuming 'in' checks for field existence
2.

Which of the following is the correct syntax for a not-in query in Firestore?

db.collection('users').where('age', ___, [18, 21, 25])
easy
A. 'notin'
B. 'not-in'
C. 'not_in'
D. 'nin'

Solution

  1. Step 1: Recall Firestore query operators

    Firestore uses exact strings for operators; the correct operator for excluding values is 'not-in'.
  2. Step 2: Check each option's validity

    'notin', 'not_in', and 'nin' are invalid syntax and will cause errors.
  3. Final Answer:

    'not-in' -> Option B
  4. Quick Check:

    Correct operator syntax = 'not-in' [OK]
Hint: Use exact operator strings: 'not-in' with hyphen [OK]
Common Mistakes:
  • Using 'notin' or 'not_in' instead of 'not-in'
  • Missing quotes around operator
  • Using unsupported operators like 'nin'
3.

Given this Firestore query:

db.collection('products').where('category', 'in', ['books', 'games']).get()

Which documents will be returned?

medium
A. Documents where category is either 'books' or 'games'
B. Documents where category is not 'books' or 'games'
C. Documents where category is exactly 'booksgames'
D. All documents regardless of category

Solution

  1. Step 1: Understand the 'in' query behavior

    The query filters documents where the 'category' field matches any value in the list ['books', 'games'].
  2. Step 2: Analyze each option

    Documents where category is either 'books' or 'games' matches the query logic. Documents where category is not 'books' or 'games' is the opposite. Documents where category is exactly 'booksgames' is a concatenation, which is incorrect. All documents regardless of category ignores the filter.
  3. Final Answer:

    Documents where category is either 'books' or 'games' -> Option A
  4. Quick Check:

    'in' query returns matching values in list [OK]
Hint: Think: 'in' means any value inside the list [OK]
Common Mistakes:
  • Confusing 'in' with 'not-in'
  • Assuming concatenation of list values
  • Ignoring the filter and expecting all documents
4.

What is wrong with this Firestore query?

db.collection('orders').where('status', 'not-in', ['shipped', 'delivered', 'pending', 'cancelled', 'new', 'processing', 'confirmed', 'refunded', 'returned', 'failed', 'on_hold']).get()

It throws an error. Why?

medium
A. The 'not-in' operator cannot be used with more than 10 values in the list
B. The field 'status' does not exist in the collection
C. The 'not-in' operator is not supported in Firestore
D. The list has more than 20 values, exceeding Firestore limits

Solution

  1. Step 1: Recall Firestore limits on 'not-in' queries

    Firestore limits 'not-in' and 'in' queries to a maximum of 10 values in the list.
  2. Step 2: Count values in the list

    The list has 11 values, exceeding the 10-value limit for 'not-in' queries.
  3. Step 3: Consider other options

    The list has more than 20 values, exceeding Firestore limits is incorrect because the list has 11 values. The 'not-in' operator is not supported in Firestore is wrong because 'not-in' is supported. The field 'status' does not exist in the collection is unrelated to the error.
  4. Final Answer:

    The 'not-in' operator cannot be used with more than 10 values in the list -> Option A
  5. Quick Check:

    'not-in' max 10 values limit causes errors [OK]
Hint: Check list size: 'not-in' max 10 values allowed [OK]
Common Mistakes:
  • Assuming 'not-in' supports unlimited list size
  • Blaming field existence without checking
  • Confusing 'not-in' support in Firestore
5.

You want to find all users whose role is neither 'admin' nor 'moderator' nor 'guest'. Which Firestore query is correct?

hard
A. db.collection('users').where('role', '!=', ['admin', 'moderator', 'guest']).get()
B. db.collection('users').where('role', 'in', ['admin', 'moderator', 'guest']).get()
C. db.collection('users').where('role', 'not-in', ['admin', 'moderator', 'guest']).get()
D. db.collection('users').where('role', 'not-in', 'admin,moderator,guest').get()

Solution

  1. Step 1: Understand the requirement

    You want users whose role is NOT any of the listed roles. This means excluding those roles.
  2. Step 2: Choose the correct operator and syntax

    The 'not-in' operator excludes documents with any value in the list. db.collection('users').where('role', 'not-in', ['admin', 'moderator', 'guest']).get() uses correct syntax with a list. db.collection('users').where('role', 'in', ['admin', 'moderator', 'guest']).get() includes only those roles, which is opposite. db.collection('users').where('role', '!=', ['admin', 'moderator', 'guest']).get() uses '!=' which does not accept lists. db.collection('users').where('role', 'not-in', 'admin,moderator,guest').get() uses a string instead of a list, which is invalid.
  3. Final Answer:

    db.collection('users').where('role', 'not-in', ['admin', 'moderator', 'guest']).get() -> Option C
  4. Quick Check:

    Use 'not-in' with list to exclude multiple values [OK]
Hint: Use 'not-in' with array to exclude multiple values [OK]
Common Mistakes:
  • Using '!=' with a list instead of a single value
  • Passing a string instead of an array for 'not-in'
  • Using 'in' instead of 'not-in' for exclusion
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