0
0
GCPcloud~20 mins

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

Choose your learning style9 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.