📖 Scenario: You are building a simple app to manage a library's book collection using Google Firestore. You want to organize the data and run queries efficiently.
🎯 Goal: Create a Firestore collection with book data, add a configuration for filtering, write a query to find books by a specific author, and finally configure a composite index to support the query.
📋 What You'll Learn
Create a Firestore collection named books with specific book documents
Add a variable to filter books by the author "Jane Austen"
Write a Firestore query to get books where author equals the filter variable
Add a composite index configuration for author and publishedYear
💡 Why This Matters
🌍 Real World
Firestore is used to store and query data in many apps. Knowing how to structure data and indexes helps apps run fast and scale well.
💼 Career
Cloud engineers and developers often configure Firestore queries and indexes to optimize app performance and cost.
Progress0 / 4 steps
1
Create the Firestore collection with book documents
Create a Firestore collection named books with these exact documents: {"title": "Pride and Prejudice", "author": "Jane Austen", "publishedYear": 1813}, {"title": "Emma", "author": "Jane Austen", "publishedYear": 1815}, and {"title": "1984", "author": "George Orwell", "publishedYear": 1949}.
GCP
Hint
Use a list named books with dictionaries for each book.
2
Add a filter variable for author
Create a variable called author_filter and set it to the string "Jane Austen" to filter books by this author.
GCP
Hint
Assign the exact string "Jane Austen" to author_filter.
3
Write a Firestore query to get books by the author filter
Write a Firestore query named query that filters the books collection where the author field equals the variable author_filter.
GCP
Hint
Use a list comprehension named query to filter books by author_filter.
4
Add a composite index configuration for author and publishedYear
Add a Firestore composite index configuration named composite_index as a dictionary with collectionId set to "books", and fields as a list of dictionaries for author ascending and publishedYear descending.
GCP
Hint
Define composite_index as a dictionary with the required keys and list of fields.
Practice
(1/5)
1. What is the main purpose of indexes in Firestore queries?
easy
A. To make queries faster and more efficient
B. To store large files like images
C. To backup data automatically
D. To encrypt data for security
Solution
Step 1: Understand what indexes do in Firestore
Indexes help Firestore find data quickly without scanning all documents.
Step 2: Relate indexes to query performance
By using indexes, queries run faster and use less resources.
Final Answer:
To make queries faster and more efficient -> Option A
Quick Check:
Indexes speed up queries = D [OK]
Hint: Indexes speed up data search in Firestore queries [OK]
Common Mistakes:
Thinking indexes store files
Confusing indexes with backups
Assuming indexes encrypt data
2. Which of the following is the correct syntax to create a simple Firestore query filtering by a field age equal to 30?
easy
A. db.collection('users').filter('age', '=', 30)
B. db.collection('users').where('age', '==', 30)
C. db.collection('users').query('age' == 30)
D. db.collection('users').find('age', 30)
Solution
Step 1: Recall Firestore query syntax
Firestore uses where(field, operator, value) to filter data.
Step 2: Match syntax with options
db.collection('users').where('age', '==', 30) uses where('age', '==', 30), which is correct syntax.
Final Answer:
db.collection('users').where('age', '==', 30) -> Option B
Quick Check:
Correct Firestore filter syntax = B [OK]
Hint: Use .where(field, '==', value) for filtering in Firestore [OK]
But it gives an error about missing index. What is the best fix?
medium
A. Change price filter to ==
B. Remove the orderBy clause
C. Create a composite index on price (ascending) and name (ascending)
D. Create a single-field index on name only
Solution
Step 1: Identify query components causing index error
The query filters by price > 100 and orders by name.
Step 2: Understand Firestore index needs for range filter + orderBy
Firestore requires a composite index on both fields with correct sort order.
Final Answer:
Create a composite index on price (ascending) and name (ascending) -> Option C
Quick Check:
Composite index needed for range filter + orderBy = C [OK]
Hint: Create composite index matching filter and orderBy fields [OK]
Common Mistakes:
Removing orderBy instead of indexing
Changing filter operator incorrectly
Creating only single-field index
5. You want to query a Firestore collection events to find documents where type is 'conference' and attendees is greater than 100, sorted by date ascending. Which index should you create to support this query?
hard
A. Composite index on type (ascending), attendees (ascending), and date (ascending)
B. Single-field index on date only
C. Composite index on type (descending) and date (ascending)
D. No index needed, Firestore supports this query by default
Solution
Step 1: Analyze query filters and sorting
The query filters by type == 'conference' and attendees > 100, and sorts by date ascending.
Step 2: Determine required index type
Multiple filters plus sorting require a composite index including all these fields with correct order.
Final Answer:
Composite index on type (ascending), attendees (ascending), and date (ascending) -> Option A
Quick Check:
Composite index covers all filters and orderBy fields = A [OK]
Hint: Include all filter and orderBy fields in composite index [OK]
Common Mistakes:
Creating only single-field index
Using wrong sort order in index
Assuming no index needed
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.