Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

114
Vistas
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
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda