📖 Scenario: You are managing a small online store's inventory using Firebase Firestore. You want to learn how to query products based on categories they belong to or exclude certain categories.
🎯 Goal: Build Firestore queries that use in and not-in operators to filter products by their categories.
📋 What You'll Learn
Create a Firestore collection reference called productsRef pointing to 'products'
Create a list variable called categoriesToInclude with exact values ['electronics', 'books', 'clothing']
Write a Firestore query called includeQuery that finds products where the 'category' field is in categoriesToInclude
Write a Firestore query called excludeQuery that finds products where the 'category' field is not in categoriesToInclude
💡 Why This Matters
🌍 Real World
Filtering products by categories is a common task in online stores to show customers relevant items.
💼 Career
Knowing how to use Firestore queries with 'in' and 'not-in' operators is essential for building efficient and user-friendly cloud-based applications.
Progress0 / 4 steps
1
Create Firestore collection reference
Create a Firestore collection reference called productsRef that points to the 'products' collection using firebase.firestore().collection('products').
Firebase
Hint
Use firebase.firestore().collection('products') to get the collection reference.
2
Create categories list
Create a constant array called categoriesToInclude with these exact string values: 'electronics', 'books', and 'clothing'.
Firebase
Hint
Use square brackets [] to create the array with the exact values.
3
Write query using 'in' operator
Write a constant called includeQuery that queries productsRef using .where('category', 'in', categoriesToInclude) to find products whose category is in the list categoriesToInclude.
Firebase
Hint
Use .where('category', 'in', categoriesToInclude) on productsRef.
4
Write query using 'not-in' operator
Write a constant called excludeQuery that queries productsRef using .where('category', 'not-in', categoriesToInclude) to find products whose category is not in the list categoriesToInclude.
Firebase
Hint
Use .where('category', 'not-in', categoriesToInclude) on productsRef.
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
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.
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.
Final Answer:
Finds documents where a field matches any value in a given list -> Option D
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?
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
Step 1: Understand the 'in' query behavior
The query filters documents where the 'category' field matches any value in the list ['books', 'games'].
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.
Final Answer:
Documents where category is either 'books' or 'games' -> Option A
Quick Check:
'in' query returns matching values in list [OK]
Hint: Think: 'in' means any value inside the list [OK]
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
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.
Step 2: Count values in the list
The list has 11 values, exceeding the 10-value limit for 'not-in' queries.
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.
Final Answer:
The 'not-in' operator cannot be used with more than 10 values in the list -> Option A
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
Step 1: Understand the requirement
You want users whose role is NOT any of the listed roles. This means excluding those roles.
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.
Final Answer:
db.collection('users').where('role', 'not-in', ['admin', 'moderator', 'guest']).get() -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.