0
0
Firebasecloud~30 mins

Query limitations and workarounds in Firebase - Mini Project: Build & Apply

Choose your learning style9 modes available
Query limitations and workarounds
📖 Scenario: You are building a simple app that stores user profiles in Firebase Firestore. You want to query users by their age and city. However, Firestore has some query limitations, such as not allowing multiple inequality filters on different fields in a single query.This project will guide you to create a Firestore query setup that works around these limitations by using composite indexes and client-side filtering.
🎯 Goal: Create a Firestore query that retrieves users filtered by age and city using a composite index and client-side filtering to work around Firestore's query limitations.
📋 What You'll Learn
Create a Firestore collection reference for 'users' with sample data
Add a configuration variable for the minimum age filter
Write a Firestore query that filters users by city and age using a composite index
Add client-side filtering to further filter users by age after the query
💡 Why This Matters
🌍 Real World
Many apps need to query databases with multiple filters but face limitations in query capabilities. This project shows how to handle such cases in Firebase Firestore.
💼 Career
Understanding Firestore query limitations and workarounds is essential for cloud developers building scalable and efficient apps using Firebase.
Progress0 / 4 steps
1
Create Firestore collection reference with sample users
Create a variable called usersRef that references the Firestore collection named 'users'. Then create a list called sampleUsers with these exact user objects: { id: 'u1', age: 25, city: 'New York' }, { id: 'u2', age: 30, city: 'Los Angeles' }, { id: 'u3', age: 22, city: 'New York' }.
Firebase
Need a hint?

Use firestore.collection('users') to get the collection reference.

2
Add minimum age filter configuration
Create a variable called minAge and set it to the number 23 to use as the minimum age filter.
Firebase
Need a hint?

Just create a variable minAge and assign 23.

3
Write Firestore query filtering by city and age
Create a variable called query that queries usersRef filtering where city equals 'New York' and age is greater than or equal to minAge. Use Firestore's where method twice to apply these filters.
Firebase
Need a hint?

Chain two where calls on usersRef for city and age.

4
Add client-side filtering for age
Create a variable called filteredUsers that filters the sampleUsers list to include only users where city is 'New York' and age is greater than or equal to minAge. Use the filter method with a function that checks these conditions.
Firebase
Need a hint?

Use sampleUsers.filter with a function checking city and age.