0
0
Firebasecloud~20 mins

Compound queries (multiple where) in Firebase - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Compound Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
service_behavior
intermediate
2:00remaining
Understanding compound queries with multiple where clauses

Consider a Firestore collection named products with documents having fields category (string) and price (number). You want to query products where category is 'electronics' and price is less than 500.

Which query will correctly return the matching documents?

Adb.collection('products').where('category', '==', 'electronics').where('price', '>', 500).get()
Bdb.collection('products').where('category', '==', 'electronics' && 'price', '<', 500).get()
Cdb.collection('products').where('category', '==', 'electronics').where('price', '<=', 500).get()
Ddb.collection('products').where('category', '==', 'electronics').where('price', '<', 500).get()
Attempts:
2 left
💡 Hint

Remember that Firestore supports chaining multiple where clauses for compound queries.

Architecture
intermediate
2:00remaining
Firestore index requirements for compound queries

You have a Firestore query with two where clauses on fields status and priority. What must you ensure for this query to work without errors?

ANo index is needed because Firestore automatically indexes all fields
BA composite index on <code>status</code> and <code>priority</code> fields must exist
COnly single-field indexes on <code>status</code> and <code>priority</code> are required
DAn index on <code>status</code> only is sufficient
Attempts:
2 left
💡 Hint

Firestore requires composite indexes for queries with multiple where clauses on different fields.

security
advanced
2:00remaining
Security rules impact on compound queries

You have Firestore security rules that allow reading documents only if userId field matches the authenticated user's ID. You run a compound query with where('userId', '==', auth.uid) and where('status', '==', 'active'). What will happen if the rules do not explicitly allow filtering by status?

AThe query will succeed but only return documents with <code>status</code> 'active' and matching <code>userId</code>
BThe query will return documents ignoring the <code>status</code> filter
CThe query will fail because Firestore security rules require all queried fields to be allowed
DThe query will succeed because <code>userId</code> matches the authenticated user
Attempts:
2 left
💡 Hint

Firestore applies security rules to each document returned by the query after server-side filtering.

Best Practice
advanced
2:00remaining
Optimizing compound queries for performance

You have a Firestore collection with millions of documents. You want to query documents with category == 'books' and rating >= 4. Which approach is best to optimize query performance?

ACreate a composite index on <code>category</code> and <code>rating</code> fields and use a compound query with both filters
BQuery all documents with <code>category == 'books'</code> and filter <code>rating >= 4</code> in client code
CUse two separate queries for each filter and merge results in client code
DCreate single-field indexes on <code>category</code> and <code>rating</code> and query only by <code>category</code>
Attempts:
2 left
💡 Hint

Firestore performs best with proper composite indexes for compound queries.

🧠 Conceptual
expert
2:00remaining
Limitations of compound queries in Firestore

Which of the following statements about Firestore compound queries with multiple where clauses is FALSE?

AAll fields used in compound queries must be indexed appropriately
BYou can combine multiple <code>where</code> clauses on different fields using logical AND
CFirestore supports OR queries natively with multiple <code>where</code> clauses
DInequality filters (<, <=, >, >=) can only be applied on a single field in a compound query
Attempts:
2 left
💡 Hint

Think about how Firestore handles logical operators in queries.