Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda