Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure firebase database with categories and subcategories?

Hello i want to create firebase database for groceries store. I have categories and under each category i have subcategories and some of these subcategories has sub-subcategories and under those there are products. how can i structure that in firebase database? firebase structure

like image 389
user1856811 Avatar asked Dec 06 '22 14:12

user1856811


1 Answers

there i would suggest this way, may be it'll helpful.

enter image description here

as you can see above image, i have a category tree, following which consists of two child nodes which Category and ParentId. In your case i will suggest get all categories where parentId equal to Zero.

firebase.database().ref("Category").orderByChild("ParentId").equalTo(0);

which will return all Categories. then when you click a category query like below

firebase.database().ref("Category").orderByChild("ParentId").equalTo("Pass your Category Id here");

which will return all sub-categories based on the Category that you choosed.

In your case, you can add another child node like SubCategoryParentId for your subcategories which has subcategories and query like below

firebase.database().ref("Category").orderByChild("SubCategoryParentId").equalTo("Pass your Subcategory Id here");

which will return all subcategories for the subcategories you choosed.

like image 186
MuruGan Avatar answered Dec 11 '22 10:12

MuruGan