Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda