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

161
Visualizações
combining multiple collection group queries

I have set up a firebase database like such:

food (collection)
  - 10/19/2021 (document)
     - Breakfast (collection)
       - Food Item 1 (document)
     - Lunch (collection)
       - Food Item 1 (document)
     - Dinner (collection)
       - Food Item 1 (document)
  - 10/20/2021 (document)
     - Breakfast (collection)
       - Food Item 1 (document)
     - Lunch (collection)
       - Food Item 1 (document)
     - Dinner (collection)
       - Food Item 1 (document)

I now want to query for all food items that match certain criteria, i.e. FoodItem.calories < 500.

How should I set up this query?

I wanted to use a Collection group query but this only seems to work for 1 meal: E.g. breakfast, lunch, or dinner but not all three.

Should I set up 3 queries, 1 per meal and then merge results, or is there a cleaner way?

const breakfast = query(collectionGroup(db, 'breakfast'), where('calories', '<=', '100'));
const querySnapshot = await getDocs(breakfast);

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Collection group queries work over all collections with a given name. Since you have named your collections after the meal they cover, you can't use a single collection group query to read across all meals.

The common solution is to use a single name for all these collections (e.g. meal) and then store the type of meal (e.g. Breakfast) in the parent document of the food items in that meal.

The alternative is to perform a separate collection group query for each meal type (e.g. Breakfast) and merge the results in your application code..

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