0
0
Firebasecloud~30 mins

Compound queries (multiple where) in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Compound queries (multiple where) in Firebase
📖 Scenario: You are managing a Firebase Firestore database for a small online bookstore. You want to find books that meet multiple conditions to help customers find the right books quickly.
🎯 Goal: Build a Firebase Firestore query that uses multiple where clauses to filter books by genre and price.
📋 What You'll Learn
Create a collection reference to books
Add a configuration variable for the maximum price filter
Write a compound query using two where clauses: one for genre and one for price
Complete the query by calling getDocs() to retrieve the filtered books
💡 Why This Matters
🌍 Real World
Filtering data with multiple conditions is common in apps like online stores, where users want to find items matching several criteria.
💼 Career
Understanding compound queries in Firestore is essential for backend and full-stack developers working with Firebase to build efficient and user-friendly applications.
Progress0 / 4 steps
1
Create a collection reference to books
Create a variable called booksRef that references the Firestore collection named books using collection(db, 'books').
Firebase
Need a hint?

Use the collection function with db and the string 'books'.

2
Add a configuration variable for the maximum price filter
Create a constant called maxPrice and set it to 20 to filter books priced at $20 or less.
Firebase
Need a hint?

Use const maxPrice = 20; to set the price limit.

3
Write a compound query using two where clauses
Create a variable called booksQuery that uses query() with booksRef and two where clauses: one where genre equals 'fiction', and another where price is less than or equal to maxPrice.
Firebase
Need a hint?

Use query() with where filters for genre and price.

4
Complete the query by calling getDocs()
Create an async function called fetchBooks that calls getDocs(booksQuery) and stores the result in a variable called querySnapshot.
Firebase
Need a hint?

Define an async function and use await getDocs(booksQuery) inside it.