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

Answer

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