Bird
Raised Fist0
Firebasecloud~20 mins

Composite index requirements in Firebase - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Composite Index Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What happens if a Firestore query requires a composite index that does not exist?

You run a Firestore query that filters on two fields and orders by a third. The required composite index is missing. What will Firestore do?

AThe query returns an empty result set without error.
BFirestore automatically creates the composite index in the background and runs the query.
CThe query runs but ignores the order by clause.
DFirestore throws an error with a direct link to create the missing composite index.
Attempts:
2 left
💡 Hint

Think about how Firestore helps developers create missing indexes.

🧠 Conceptual
intermediate
2:00remaining
Why are composite indexes required for some Firestore queries?

Firestore requires composite indexes for queries that:

  • Filter on multiple fields
  • Order by fields not included in single-field indexes
  • Use range filters on multiple fields

Which reason best explains why composite indexes are needed?

ATo speed up queries by pre-sorting data on multiple fields together.
BTo reduce Firestore storage costs by combining indexes.
CTo allow queries to run without any network calls.
DTo automatically backup data before queries run.
Attempts:
2 left
💡 Hint

Think about how indexes help databases find data faster.

📝 Syntax
advanced
2:00remaining
Which Firestore index definition JSON correctly defines a composite index on fields 'status' (ascending) and 'createdAt' (descending)?

Choose the valid JSON snippet for a composite index on 'status' ascending and 'createdAt' descending.

A{ "collectionGroup": "orders", "queryScope": "COLLECTION", "fields": [{"fieldPath": "status", "order": "ASC"}, {"fieldPath": "createdAt", "order": "DESC"}] }
B{ "collection": "orders", "fields": [{"fieldPath": "status", "order": "ASCENDING"}, {"fieldPath": "createdAt", "order": "DESCENDING"}] }
C{ "collectionGroup": "orders", "queryScope": "COLLECTION", "fields": [{"fieldPath": "status", "order": "ASCENDING"}, {"fieldPath": "createdAt", "order": "DESCENDING"}] }
D{ "collectionGroup": "orders", "queryScope": "COLLECTION", "fields": [{"fieldPath": "status", "order": "ascending"}, {"fieldPath": "createdAt", "order": "descending"}] }
Attempts:
2 left
💡 Hint

Check the exact property names and values Firestore expects in index definitions.

❓ optimization
advanced
2:00remaining
How can you minimize the number of composite indexes needed for multiple queries on the same collection?

You have several queries on a Firestore collection filtering and ordering by different combinations of fields. What is the best way to reduce the total composite indexes you must create?

ACreate separate composite indexes for each query to avoid conflicts.
BCreate one composite index that includes all fields used in any query, ordered by frequency of use.
CUse only single-field indexes and avoid composite indexes entirely.
DCreate composite indexes only for the most complex queries and let others run without indexes.
Attempts:
2 left
💡 Hint

Think about how composite indexes can cover multiple queries if designed carefully.

🔧 Debug
expert
2:00remaining
Why does this Firestore query fail despite having a composite index defined?

Given a composite index on fields 'category' (ascending) and 'price' (descending), this query filters where category = 'books' and orders by price ascending. Why does it fail?

db.collection('products').where('category', '==', 'books').orderBy('price', 'asc')
AThe composite index order for 'price' is descending but the query orders ascending, causing a mismatch.
BFirestore does not support ordering by fields with equality filters.
CThe query must include an orderBy on 'category' before ordering by 'price'.
DThe composite index must include a filter on 'price' as well.
Attempts:
2 left
💡 Hint

Check if the order directions in the query match the index definition.

Practice

(1/5)
1. What is the main reason Firebase requires a composite index?
easy
A. To encrypt data for security
B. To store data in multiple locations
C. To backup data automatically
D. To efficiently query data using multiple fields together

Solution

  1. Step 1: Understand composite index purpose

    Composite indexes allow Firebase to quickly search using more than one field at the same time.
  2. Step 2: Compare options to purpose

    Only To efficiently query data using multiple fields together matches this purpose; others describe unrelated features.
  3. Final Answer:

    To efficiently query data using multiple fields together -> Option D
  4. Quick Check:

    Composite index = multi-field query speed [OK]
Hint: Composite indexes speed up queries with multiple fields [OK]
Common Mistakes:
  • Confusing composite indexes with data backup
  • Thinking composite indexes encrypt data
  • Believing composite indexes store data in multiple places
2. Which of the following is the correct way to define a composite index in Firebase JSON?
easy
A. "indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": [{"fieldPath": "age", "order": "ASCENDING"}, {"fieldPath": "name", "order": "DESCENDING"}]}]
B. "indexes": [{"collection": "users", "fields": ["age", "name"]}]
C. "indexes": [{"collectionGroup": "users", "fields": [{"fieldPath": "age"}, {"fieldPath": "name"}]}]
D. "indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": ["age", "name"]}]

Solution

  1. Step 1: Check JSON structure for composite index

    Correct composite index JSON includes collectionGroup, queryScope, and fields with fieldPath and order.
  2. Step 2: Validate each option

    Only "indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": [{"fieldPath": "age", "order": "ASCENDING"}, {"fieldPath": "name", "order": "DESCENDING"}]}] has all required keys and correct field definitions with order specified.
  3. Final Answer:

    "indexes": [{"collectionGroup": "users", "queryScope": "COLLECTION", "fields": [{"fieldPath": "age", "order": "ASCENDING"}, {"fieldPath": "name", "order": "DESCENDING"}]}] -> Option A
  4. Quick Check:

    Composite index JSON needs fields with order [OK]
Hint: Composite index JSON needs fields with order and collectionGroup [OK]
Common Mistakes:
  • Omitting order in fields
  • Using 'collection' instead of 'collectionGroup'
  • Listing fields as simple strings without objects
3. Given this query in Firebase: db.collection('products').where('category', '==', 'books').orderBy('price', 'asc').orderBy('rating', 'desc'), what composite index is required?
medium
A. Index on fields: category (DESC), price (ASC), rating (ASC)
B. Index on fields: price (ASC), rating (DESC)
C. Index on fields: category (ASC), price (ASC), rating (DESC)
D. No composite index needed

Solution

  1. Step 1: Identify query filters and orderBy fields

    The query filters on 'category' and orders by 'price' ascending and 'rating' descending.
  2. Step 2: Determine composite index fields and order

    Composite index must include 'category' (filter), then 'price' (asc), then 'rating' (desc) in that order.
  3. Final Answer:

    Index on fields: category (ASC), price (ASC), rating (DESC) -> Option C
  4. Quick Check:

    Filter + orderBy fields = composite index fields [OK]
Hint: Include all filter and orderBy fields with correct order [OK]
Common Mistakes:
  • Ignoring filter field in index
  • Wrong order or direction of fields
  • Assuming no index needed for multiple orderBy
4. You get an error saying a composite index is missing for this query: db.collection('orders').where('status', '==', 'shipped').orderBy('date', 'desc'). Which fix is correct?
medium
A. Create a composite index on fields: status (ASC), date (DESC)
B. Create a composite index on fields: date (DESC), status (ASC)
C. Create a single-field index on date only
D. No index needed; fix query syntax

Solution

  1. Step 1: Analyze query filter and orderBy

    The query filters on 'status' and orders by 'date' descending.
  2. Step 2: Determine correct composite index order

    Composite index must start with filter field 'status' (ASC by default), then 'date' (DESC) as orderBy.
  3. Final Answer:

    Create a composite index on fields: status (ASC), date (DESC) -> Option A
  4. Quick Check:

    Filter field first, then orderBy fields in index [OK]
Hint: Filter field first in index, then orderBy fields [OK]
Common Mistakes:
  • Reversing field order in index
  • Creating single-field index only
  • Ignoring error and changing query syntax
5. You want to query a collection 'events' filtering by 'location' and 'type', and sorting by 'date' ascending. Which composite index definition is correct?
hard
A. Index on fields: date (ASC), location (ASC), type (ASC)
B. Index on fields: location (ASC), type (ASC), date (ASC)
C. Index on fields: location (DESC), type (DESC), date (ASC)
D. No composite index needed if only filtering

Solution

  1. Step 1: Identify query filter and sort fields

    The query filters on 'location' and 'type', and sorts by 'date' ascending.
  2. Step 2: Determine composite index order

    Composite index must list filter fields first in any order, then the sort field last with ascending order.
  3. Step 3: Validate options

    Index on fields: location (ASC), type (ASC), date (ASC) matches filter fields first (location, type) then date ascending. Index on fields: date (ASC), location (ASC), type (ASC) starts with date, which is incorrect. Index on fields: location (DESC), type (DESC), date (ASC) uses descending for filters, which is wrong. No composite index needed if only filtering is incorrect because sorting requires composite index.
  4. Final Answer:

    Index on fields: location (ASC), type (ASC), date (ASC) -> Option B
  5. Quick Check:

    Filters first, then sort field in index [OK]
Hint: Filter fields first, then sort field ascending in index [OK]
Common Mistakes:
  • Placing sort field before filters in index
  • Using descending order for filter fields
  • Assuming no index needed when sorting
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