📖 Scenario: You are managing a Firebase Firestore database for a small online bookstore. You want to find books that meet multiple conditions to help customers find the right books quickly.
🎯 Goal: Build a Firebase Firestore query that uses multiple where clauses to filter books by genre and price.
📋 What You'll Learn
Create a collection reference to books
Add a configuration variable for the maximum price filter
Write a compound query using two where clauses: one for genre and one for price
Complete the query by calling getDocs() to retrieve the filtered books
💡 Why This Matters
🌍 Real World
Filtering data with multiple conditions is common in apps like online stores, where users want to find items matching several criteria.
💼 Career
Understanding compound queries in Firestore is essential for backend and full-stack developers working with Firebase to build efficient and user-friendly applications.
Progress0 / 4 steps
1
Create a collection reference to books
Create a variable called booksRef that references the Firestore collection named books using collection(db, 'books').
Firebase
Hint
Use the collection function with db and the string 'books'.
2
Add a configuration variable for the maximum price filter
Create a constant called maxPrice and set it to 20 to filter books priced at $20 or less.
Firebase
Hint
Use const maxPrice = 20; to set the price limit.
3
Write a compound query using two where clauses
Create a variable called booksQuery that uses query() with booksRef and two where clauses: one where genre equals 'fiction', and another where price is less than or equal to maxPrice.
Firebase
Hint
Use query() with where filters for genre and price.
4
Complete the query by calling getDocs()
Create an async function called fetchBooks that calls getDocs(booksQuery) and stores the result in a variable called querySnapshot.
Firebase
Hint
Define an async function and use await getDocs(booksQuery) inside it.
Practice
(1/5)
1. What does a compound query with multiple .where() filters do in Firebase Firestore?
easy
A. It deletes documents that match the conditions.
B. It finds documents that match any one of the specified conditions.
C. It finds documents that match all the specified conditions.
D. It updates documents that match the conditions.
Solution
Step 1: Understand the purpose of .where() filters
Each .where() filter narrows down the documents by a condition.
Step 2: Combine multiple filters in a compound query
Multiple .where() filters together find documents matching all conditions (AND logic).
Final Answer:
It finds documents that match all the specified conditions. -> Option C
Quick Check:
Compound query = all conditions matched [OK]
Hint: Multiple .where() means all conditions must be true [OK]
Common Mistakes:
Thinking it matches any condition (OR logic)
Confusing query with update or delete
Assuming it returns all documents
2. Which of the following is the correct syntax to create a compound query with two .where() filters in Firebase Firestore?
easy
A. db.collection('users').where(['age', '>=', 18], ['status', '==', 'active'])
B. db.collection('users').where('age', '>=', 18, 'status', '==', 'active')
C. db.collection('users').filter('age', '>=', 18).filter('status', '==', 'active')
D. db.collection('users').where('status', '==', 'active').where('age', '>=', 18)
Solution
Step 1: Review Firestore query chaining syntax
Each .where() is chained separately with three arguments: field, operator, value.
Step 2: Identify correct chaining of two .where() calls
db.collection('users').where('status', '==', 'active').where('age', '>=', 18) correctly chains two .where() calls with proper arguments.
Final Answer:
db.collection('users').where('status', '==', 'active').where('age', '>=', 18) -> Option D
Quick Check:
Separate .where() calls chained = correct syntax [OK]
Hint: Chain separate .where() calls with 3 arguments each [OK]
Firestore error usually provides a link to create the needed composite index.
Final Answer:
Create a composite index for 'category' and 'price' fields as requested by Firestore. -> Option A
Quick Check:
Missing index error = create composite index [OK]
Hint: Create composite index when Firestore asks [OK]
Common Mistakes:
Removing filters instead of creating index
Ignoring Firestore error message
Trying to replace .where() with .orderBy() incorrectly
5. You want to query a Firestore collection 'events' for documents where type is 'conference', attendees is greater than 100, and location is 'NYC'. Which of the following is the best approach to write this compound query and ensure it works efficiently?
hard
A. Chain three .where() filters for each condition and create a composite index for these fields.
B. Use a single .where() with an array of conditions for all fields.
C. Query only by type and filter the rest in client code.
D. Use .orderBy() on all fields instead of .where().
Solution
Step 1: Write the compound query with multiple .where() filters
Use .where('type', '==', 'conference'), .where('location', '==', 'NYC'), and .where('attendees', '>', 100) chained together.
Step 2: Ensure Firestore has a composite index for these fields
Because multiple filters on different fields are used, create a composite index to optimize query performance and avoid errors.
Final Answer:
Chain three .where() filters and create a composite index for these fields. -> Option A
Quick Check:
Multiple filters + composite index = efficient query [OK]
Hint: Chain .where() and create composite index for multiple fields [OK]
Common Mistakes:
Trying to put all conditions in one .where() call
Filtering on client side instead of query
Using .orderBy() instead of .where() for filtering
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.