Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
How can I prevent having multiple combinations of Firestore Indexes

Basically, I have a query from Firestore, and some of the filter fields may be empty, in that case the specific query will be ignored,Since inequality queries with multiple fields requires indexes, what's the best way to create indexes of such query without going through all possible combinations.

e.g, 'country' and 'budget_high', 'gender' and 'budget_high', 'age' and 'budget_high', 'country, gender' and 'budget_high' and so on.

This is my query.

if (country !== "") query = query.where("country", "==", country);
  if (gender !== "") query = query.where("gender", "==", gender);
  if (age !== "") query = query.where("age", "==", age);
  if (religion !== "") query = query.where("religion", "==", religion);
  if (budget_high !== "") query = query.where("budget_high", "<=", Number(budget_high));
  return query
    .get()
    .then((querySnapshot) => {
      var data = [];
      querySnapshot.forEach((doc) => {
        let user_data = doc.data();
        user_data.created_at = `${user_data.created_at.toDate()}`;
        data.push(user_data);
      });
      return data;
    })
    .catch((error) => {
      console.log("Error getting documents: ", error);
      return [];
    });
};

Here are my Single Field Exemptions for collection (not collection group) enter image description here

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

First, we need a field that will be present in all queries. You can orderBy one of the fields inorder for it to always be present (budget_high in this case since you are calling inequality query on it).

So create 4 composite indexes.

  1. budget_high and country ASC
  2. budget_high and gender ASC
  3. budget_high and age ASC
  4. budget_high and religion ASC

This will effectively handle your query combinations.

Read the merging index docs

about 4 years ago · Juan Pablo Isaza Report

0

Following up on @Peter O. response, the order of the fields in the indexes is important. Please try the following:

  1. country ASC and budget_high
  2. gender ASC and budget_high
  3. age ASC and budget_high
  4. religion ASC and budget_high

I hope this helps.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!