I have an category array of json data with the following structure.
My goal is when i pass the 'id' as route parameter,
- I should get the category matching the id.
- If the recieved category object has field
hasSubCategory=false
,then i should return the category document as response. - But if the recieved category object has field
hasSubCategory=true
,I should again perform the db operation to query all the sub categories by checking if the object id matchesparentId
of the json array.In this case,if the sub categories again havehasSubCategory=true
,another query is required to check the id of current obj withparentId
of json array.When recieved subcategory also havehasSubCategory=true
,another query is required.So it should query untillhasSubCategory=false
.
Briefly,If the following object has hasSubCategory=true
,I should check the entire json data whether object id matches any of the parentId.
In the above json,we can see that first object's _id is represented in other object's parentId .The _id of sub category can be used as parentId of any other object.Thus sub category may have subcategories which uses its id as parentId.
The categories may have deeper levels of subcategories and some donot.I am trying to make it dynamic.
The End result what i am trying to get is all the category id's which donot have any further subcategories (hasSubCategory=false
)