Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
how to go deep into a tree with javascript?

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!