I'm looking display categories tree. But I can't make the route in depth. unlimitedly, I am only using for loop.
I'm use nodejs, expressjs, mongoose then:
Category model
var CategorySchema = new Schema({
name: {
type: String,
required: true,
select: true
},
_mother_category : {
type: mongoose.Schema.Types.ObjectId,
ref: 'Category',
required: false,
select: true
},})
So my for loop it's:
// find mother cats
let mother_cat = await Category.find({ '_mother_category' : { $exists: false}}),
subcat;
// single deep
for (const mc of mother_cat) {
subcat = await Category.find({ '_mother_category' : mc['_id'] })
console.log(subcat['length'])
}
But this wouldn't help me if I had more depth, and doing several for loops I don't think so either.
Any suggestion?
Rgds