Bird
Raised Fist0
GCPcloud~20 mins

Firestore queries and indexes in GCP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Firestore Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ service_behavior
intermediate
2:00remaining
Firestore Query Result Count
You have a Firestore collection named users with documents containing fields age (number) and city (string). You run this query:

db.collection('users').where('age', '>', 25).where('city', '==', 'Seattle').get()

What will be the number of documents returned if the collection has:
- 5 users aged 30 in Seattle
- 3 users aged 20 in Seattle
- 4 users aged 40 in New York
- 2 users aged 50 in Seattle?
A10
B5
C2
D7
Attempts:
2 left
💡 Hint
Remember the query filters on age > 25 and city == 'Seattle'.
❓ Architecture
intermediate
2:00remaining
Firestore Composite Index Requirement
You want to run this Firestore query:

db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc').limit(10)

Which statement about indexes is true?
AA composite index on 'status' and 'date' is required for this query to work.
BFirestore automatically uses a single-field index on 'status' and a composite index on 'status' and 'date' is not needed.
CNo indexes are needed because Firestore scans all documents.
DOnly a single-field index on 'date' is required.
Attempts:
2 left
💡 Hint
Think about how Firestore handles queries with filters and orderBy on different fields.
❓ security
advanced
2:00remaining
Firestore Query and Security Rules
Given this Firestore security rule:

allow read: if request.auth.uid == resource.data.ownerId;

What happens if a user runs a query filtering documents by ownerId equal to their UID?
AThe query returns only documents where ownerId matches the user's UID.
BThe query returns all documents regardless of ownerId because security rules do not filter query results.
CThe query fails because Firestore requires an index on ownerId for security rules.
DThe query returns documents but only those the user is authorized to read.
Attempts:
2 left
💡 Hint
Security rules filter access after the query runs, but queries must match rules to succeed.
✅ Best Practice
advanced
2:00remaining
Optimizing Firestore Query Performance
You have a Firestore collection with millions of documents. You want to query documents where category is 'books' and price is less than 20, ordered by price. Which approach is best for performance?
AUse two separate queries: one filtering category and one filtering price, then merge results in code.
BCreate a composite index on 'category' and 'price' and use a query filtering category and price with orderBy price.
CQuery all documents and filter results in the application code.
DCreate a single-field index on 'price' only and filter category in code.
Attempts:
2 left
💡 Hint
Indexes help Firestore quickly find matching documents without scanning all data.
🧠 Conceptual
expert
2:00remaining
Firestore Query Limitations and Indexes
Which Firestore query is NOT possible without creating a custom composite index?
AQuery documents where 'status' == 'active' and order by 'createdAt' descending.
BQuery documents where 'category' == 'electronics' and 'price' < 500 ordered by 'price'.
CQuery documents where 'age' > 30 and 'score' < 100.
DQuery documents where 'type' == 'premium' ordered by 'lastLogin' ascending.
Attempts:
2 left
💡 Hint
Consider Firestore's support for range filters on multiple 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

  1. Step 1: Understand what indexes do in Firestore

    Indexes help Firestore find data quickly without scanning all documents.
  2. Step 2: Relate indexes to query performance

    By using indexes, queries run faster and use less resources.
  3. Final Answer:

    To make queries faster and more efficient -> Option A
  4. 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

  1. Step 1: Recall Firestore query syntax

    Firestore uses where(field, operator, value) to filter data.
  2. Step 2: Match syntax with options

    db.collection('users').where('age', '==', 30) uses where('age', '==', 30), which is correct syntax.
  3. Final Answer:

    db.collection('users').where('age', '==', 30) -> Option B
  4. Quick Check:

    Correct Firestore filter syntax = B [OK]
Hint: Use .where(field, '==', value) for filtering in Firestore [OK]
Common Mistakes:
  • Using .filter instead of .where
  • Using == inside query string
  • Using .find which is not Firestore syntax
3. Given this Firestore query:
db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc')

What must you do to avoid an error when running this query?
medium
A. Remove the orderBy clause
B. Create a single-field index on date only
C. No index needed, Firestore handles it automatically
D. Create a composite index on status and date

Solution

  1. Step 1: Understand query with multiple filters and sorting

    This query filters by status and sorts by date.
  2. Step 2: Know Firestore index requirements

    Firestore requires a composite index for queries with multiple filters or sorting on different fields.
  3. Final Answer:

    Create a composite index on status and date -> Option D
  4. Quick Check:

    Composite index needed for multiple filters + orderBy = A [OK]
Hint: Use composite indexes for queries with filters and sorting [OK]
Common Mistakes:
  • Assuming single-field index is enough
  • Thinking Firestore auto-creates all indexes
  • Removing orderBy to fix error
4. You wrote this Firestore query:
db.collection('products').where('price', '>', 100).orderBy('name')

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

  1. Step 1: Identify query components causing index error

    The query filters by price > 100 and orders by name.
  2. Step 2: Understand Firestore index needs for range filter + orderBy

    Firestore requires a composite index on both fields with correct sort order.
  3. Final Answer:

    Create a composite index on price (ascending) and name (ascending) -> Option C
  4. 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

  1. Step 1: Analyze query filters and sorting

    The query filters by type == 'conference' and attendees > 100, and sorts by date ascending.
  2. Step 2: Determine required index type

    Multiple filters plus sorting require a composite index including all these fields with correct order.
  3. Final Answer:

    Composite index on type (ascending), attendees (ascending), and date (ascending) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard