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?
Remember that Firestore supports chaining multiple where clauses for compound queries.
Option D correctly chains two where clauses: one for category equality and one for price less than 500. Option D is invalid syntax. Option D uses '<=' which includes 500, not strictly less. Option D queries for price greater than 500, which is incorrect.
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?
Firestore requires composite indexes for queries with multiple where clauses on different fields.
Firestore automatically creates single-field indexes but requires a composite index for compound queries involving multiple fields. Without it, the query will fail with an error prompting index creation.
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?
Firestore applies security rules to each document returned by the query after server-side filtering.
Option A is correct. The query succeeds and returns only documents matching both conditions because Firestore executes the query server-side, and all returned documents satisfy the security rule (userId matches). Security rules do not need to explicitly reference all query filter fields; they are checked per returned document.
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?
Firestore performs best with proper composite indexes for compound queries.
Option A uses a composite index allowing Firestore to efficiently execute the compound query server-side. Options B and C cause large data transfers and client-side filtering, which is inefficient. Option A does not filter by rating server-side.
Which of the following statements about Firestore compound queries with multiple where clauses is FALSE?
Think about how Firestore handles logical operators in queries.
Firestore compound queries combine multiple where clauses with logical AND. It does not support OR queries natively with multiple where clauses. OR queries require different approaches like in or array-contains-any or multiple queries client-side.