📖 Scenario: You are building a simple app to manage a list of books in a Firebase Firestore database. You want to store book details and then query the database to find books by a specific author.
🎯 Goal: Create a Firestore collection with book data, add a configuration variable for the author to search, write a query to filter books by that author, and complete the query execution to get the results.
📋 What You'll Learn
Create a Firestore collection named books with exact book entries
Add a variable called authorToFind with the exact author name to filter
Write a Firestore query using where to filter books by authorToFind
Complete the query execution using get() to retrieve the filtered books
💡 Why This Matters
🌍 Real World
Filtering data in Firestore is common in apps that need to show users specific information, like books by an author or products in a category.
💼 Career
Knowing how to query and filter Firestore data is essential for frontend and backend developers working with Firebase to build responsive and dynamic applications.
Progress0 / 4 steps
1
Create the initial Firestore collection data
Create a Firestore collection named books and add these exact book documents with fields: title and author. The books are: { title: 'The Great Gatsby', author: 'F. Scott Fitzgerald' }, { title: '1984', author: 'George Orwell' }, and { title: 'To Kill a Mockingbird', author: 'Harper Lee' }.
Firebase
Hint
Use a constant array named books with objects for each book.
2
Add the author filter variable
Add a variable called authorToFind and set it exactly to the string 'George Orwell' to use as the filter for querying books.
Firebase
Hint
Declare a constant named authorToFind with the exact string value.
3
Write the Firestore query to filter books by author
Write a Firestore query called queryByAuthor that uses collection(db, 'books') and where('author', '==', authorToFind) to filter books by the authorToFind variable.
Firebase
Hint
Use query() with collection() and where() to filter by author.
4
Complete the query execution to get filtered books
Complete the Firestore query execution by calling getDocs(queryByAuthor) and assign the result to a variable called querySnapshot.
Firebase
Hint
Use await getDocs(queryByAuthor) and assign it to querySnapshot.
Practice
(1/5)
1. What does the where method do in a Firestore query?
easy
A. Limits the number of documents returned
B. Sorts documents in ascending order
C. Filters documents based on a condition
D. Deletes documents matching a condition
Solution
Step 1: Understand the purpose of where
The where method is used to filter documents by specifying a condition on a field.
Step 2: Compare with other query methods
orderBy sorts documents, limit restricts count, and deletion is unrelated.
Final Answer:
Filters documents based on a condition -> Option C
Quick Check:
where = filter condition [OK]
Hint: Remember: where filters, orderBy sorts, limit restricts [OK]
Common Mistakes:
Confusing where with orderBy
Thinking where limits results
Assuming where deletes data
2. Which of the following is the correct syntax to filter documents where the field age is greater than 18 in Firestore?
easy
A. query.where('age', '>', 18)
B. query.where('age' > 18)
C. query.where('age', 'greater', 18)
D. query.where('age', '>=', '18')
Solution
Step 1: Check Firestore where syntax
The correct syntax uses three arguments: field name, operator as string, and value.
Step 2: Validate each option
query.where('age', '>', 18) matches correct syntax. query.where('age' > 18) misses quotes and arguments. query.where('age', 'greater', 18) uses invalid operator. query.where('age', '>=', '18') compares number to string.
Final Answer:
query.where('age', '>', 18) -> Option A
Quick Check:
where(field, operator, value) correct format [OK]
Hint: Use quotes around operator and field name [OK]