Joe Benton
Joe Benton

Reputation: 3753

Firebase querying relational data

I am using Firebase and have a relational structure, that has 'categories' and 'recipes' in a many-to-many relationship as follows:

{
 "categories": {
   "<category_id_1>": {
     "name": "Breakfast",
     "imageUrl": "http://www.image.com/url",
     "recipes": {
       "<recipe_id_1>": true,
       "<recipe_id_2>": true
     }
   }
 },
 "recipes": {
   "<recipe_id_1>": {
     "title": "Sweet Potato & White Bean Chilli",
     "cookingTime": 50,
     "portionSize": "4-6",
     "difficulty": "Easy",
     "categories": {
       "<category_id_1>": true,
       "<category_id_2>": true
     }
   }
 }
}

I can get the recipes in a category by going to /categories/<category_id_1>/recipes and looping through to get the references to each recipe: e.g. /recipes/<recipe_id_1>.

However, what I am struggling to work out is how I can then query/filter recipes that are in the Breakfast category that have difficulty = "Easy".

What would be your suggestions on doing a query on relational data?

Upvotes: 0

Views: 139

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598648

As usual in NoSQL, you'll need to model your data for the use-case you want. So if you want to get a list of easy breakfast recipes, you should store a list of easy breakfast recipes:

{
 "categories_complexities": {
   "Breakfast_easy": {
     "<recipe_id_1>": true,
     "<recipe_id_7>": true
   }
 },
}

Note that I've unnested your data: don't mix category metadata with the list of recipes for that category.

Upvotes: 1

Related Questions