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

155
Views
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 answers
Answer question

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 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!