0
0
Firebasecloud~30 mins

Composite index requirements in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Composite Index Requirements in Firebase
📖 Scenario: You are building a simple app to store and retrieve information about books in a library. Each book has a title, author, and year of publication. You want to be able to quickly find books by author and year together.
🎯 Goal: Create a Firebase Firestore database structure and define a composite index that allows efficient queries filtering by author and year.
📋 What You'll Learn
Create a collection called books with documents containing fields title, author, and year
Add a configuration variable called indexConfig that defines a composite index on author and year
Write a query that uses the composite index to find books by a specific author and year
Complete the Firestore index configuration JSON with the correct collection and fields
💡 Why This Matters
🌍 Real World
Composite indexes in Firestore help apps quickly find data when filtering by multiple fields, like searching books by author and year.
💼 Career
Understanding composite indexes is important for database optimization roles and backend development using Firebase.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a collection called books and add three documents with these exact fields and values: { title: "The Great Gatsby", author: "F. Scott Fitzgerald", year: 1925 }, { title: "1984", author: "George Orwell", year: 1949 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960 }.
Firebase
Need a hint?

Use an array of objects named books with the exact titles, authors, and years.

2
Define the composite index configuration
Create a variable called indexConfig that defines a composite index for the books collection on the fields author and year, both in ascending order.
Firebase
Need a hint?

Use the exact variable name indexConfig and specify collectionGroup as "books" with fields author and year ascending.

3
Write a query using the composite index
Write a Firestore query called queryBooks that finds all books where author equals "George Orwell" and year equals 1949, using the books collection.
Firebase
Need a hint?

Use firestore.collection("books") and chain two where filters for author and year.

4
Complete the Firestore index configuration JSON
Complete the Firestore index configuration JSON object called firestoreIndex that includes the collectionId as "books" and the fields array with author and year both set to ascending order. This JSON is used to deploy the composite index.
Firebase
Need a hint?

Use the exact variable name firestoreIndex and include collectionId and fields with the correct field paths and order.