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?
CodePudding user response:
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..