Change structure of materialize path to children

Hello Can any one help me to change the structure of that array from materialize path to this structure

[
 {name : 'Electronic' , path: ''},
    
 {name :'Mobile' , path :'/Electronic'}, 
 {name :'LG' , path :'/Electronic/Mobile'},
 {name :'S330' , path :'/Electronic/Mobile/LG'},
 {name :'Samsung' , path :'/Electronic/Mobile'},
 {name :'Galaxy 10' , path :'/Electronic/Mobile/Samsung'},
    
 {name : 'Laptop' , path: '/Electronic'},
 {name :'HP' , path :'/Electronic/Laptop'}, 
 {name :'Pavilion 2000' , path :'/Electronic/Mobile/HP'},
 {name :'DELL' , path :'/Electronic/Laptop'}, 
 {name :'D2000' , path :'/Electronic/Mobile/DELL'},
    
 {name : 'Clothes' , path: ''},
    
 {name :'Men' , path :'/Clothes'}, 
 {name :'Socks' , path :'/Clothes/Men'},
 {name :'Pants' , path :'/Clothes/Men'}, 
     
    
 {name :'Women' , path :'/Clothes'}, 
 {name :'Skirt' , path :'/Clothes/Women'},
 {name :'Hat' , path :'/Clothes/Women'},*
]


[
 {
  name:'Electronic', children:[
     {name:'Mobile', children:['LG', 'Samsung']},
     {name:'Laptop', children:['HP', 'DELL']}
  ]
 },
{
 name:'Clothes', children:[
    {name:'Men', children:['Socks', 'Pants']},
    {name:'Women', children:['Skirt', 'DELL']}
  ]
 },
]

or if this is not a good idea can any body help me to figure out how to work with nested menu for materialize path please i get stack please any help ?

Hi @Hamza_Miloud_Amar,

Welcome to MongoDB community.

I believe you need to use $split operator to split the “/” delimiter and iterate over the array in an aggregation command.

If you need to preserve this to a target you can use $merge stage or $out to a new collection.

Now the way you should organise your hirerchical data is related to the way you access/modify the data.

For example, If you need to access the tree for every root it makes sense to store it together as long as you not cross 16MB per doc.

However, if you need to get only subpart of the tree / explode them on clicks it might make sense to store them in pointer docs.

Suggest reading this:

Best
Pavel

1 Like

Hello

Dynamic documents are hard,and nested dynamic documents are even harder
Dynamic meaning unknown keys/nested level etc
Its very hard to create/add/access/search on them

Static nested documents are easier,but still hard
Its hard to search on them,things like object to array and back make things complicated
On trees we need easy way to get parents/children

Alternative to this is using arrays,containing children/parents references
Dynamic/Static arrays are easy to create/add/access/search on them
Tree structure is saved in arrays,not as embedded documents

Something like this maybe

Products (leaves)
{
 name  'Pavilion 2000'
 brand  HP
}

Brands (last level before leaves)
{
 name HP
 parents [laptops,mobile]          //categories
 children  ['Pavilion 2000', .....]  //products
}

Categories (internal nodes)
{
 name Electronic
 parents []
 children [laptop,mobile]
}

There is university course that has videos on how to model trees in MongoDB
M320 Data modelling

1 Like