0
0
Firebasecloud~3 mins

Why Compound queries (multiple where) in Firebase? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could find exactly what you want in seconds, no matter how big your data is?

The Scenario

Imagine you have a big list of friends in your phone, and you want to find those who live in New York and like pizza. You try to look through the list one by one, checking each friend's city and favorite food manually.

The Problem

This manual search takes a long time and you might miss some friends or make mistakes. It's slow and frustrating, especially if your list grows bigger every day.

The Solution

Compound queries let you ask the database to find friends who meet both conditions at once. It's like telling your phone: "Show me friends in New York and who like pizza," and it quickly gives you the right list without extra work.

Before vs After
Before
db.collection('friends').where('city', '==', 'New York').get().then(snapshot => {
  snapshot.forEach(doc => {
    if(doc.data().favoriteFood === 'pizza') {
      console.log(doc.data())
    }
  })
})
After
db.collection('friends').where('city', '==', 'New York').where('favoriteFood', '==', 'pizza').get().then(snapshot => {
  snapshot.forEach(doc => console.log(doc.data()))
})
What It Enables

Compound queries make it easy to find exactly what you want from large data, saving time and avoiding mistakes.

Real Life Example

A food delivery app uses compound queries to show users restaurants that are open now and deliver to their area, making ordering fast and simple.

Key Takeaways

Manual filtering is slow and error-prone.

Compound queries combine multiple conditions in one search.

This makes data retrieval faster and more accurate.