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

225
Vistas
Trying to write a recursive asynchronous search in JavaScript

I am trying to write some code that searches through a bunch of objects in a MongoDB database. I want to pull the objects from the database by ID, then those objects have ID references. The program should be searching for a specific ID through this process, first getting object from id, then ids from the object.

async function objectFinder(ID1, ID2, depth, previousList = []) {
    let route = []
    if (ID1 == ID2) {
        return [ID2]
    } else {
        previousList.push(ID1)
        let obj1 = await findObjectByID(ID1)
        let connectedID = obj1.connections.concat(obj1.inclusions) //creates array of both references to object and references from object
        let mapPromises = connectedID.map(async (id) => {
            return findID(id) //async function
        })
        let fulfilled = await Promise.allSettled(mapPromises)
        let list = fulfilled.map((object) => {
            return object.value.main, object.value.included
        })
        list = list.filter(id => !previousList.includes(id))
        for (id of list) {
            await objectFinder(id, ID2, depth - 1, previousList).then(result => {
                route = [ID1].concat(result)
                if (route[route.length - 1] == ID2) {
                    return route
                }})
        }
    }
    if (route[route.length - 1] == ID2) {
        return route
    }
}

I am not sure how to make it so that my code works like a tree search, with each object and ID being a node.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I didn't look too much into your code as I strongly believe in letting your database do the work for you if possible.

In this case Mongo has the $graphLookup aggregation stage, which allows recursive lookups. here is a quick example on how to use it:

db.collection.aggregate([
  {
    $match: {
      _id: 1,
      
    }
  },
  {
    "$graphLookup": {
      "from": "collection",
      "startWith": "$inclusions",
      "connectFromField": "inclusions",
      "connectToField": "_id",
      "as": "matches",
      
    }
  },
  {
    //the rest of the pipeline is just to restore the original structure you don't need this
    $addFields: {
      matches: {
        "$concatArrays": [
          [
            {
              _id: "$_id",
              inclusions: "$inclusions"
            }
          ],
          "$matches"
        ]
      }
    }
  },
  {
    $unwind: "$matches"
  },
  {
    "$replaceRoot": {
      "newRoot": "$matches"
    }
  }
])

Mongo Playground

If for whatever reason you want to keep this in code then I would take a look at your for loop:

for (id of list) {
    await objectFinder(id, ID2, depth - 1, previousList).then(result => {
        route = [ID1].concat(result);
        if (route[route.length - 1] == ID2) {
            return route;
        }
    });
}

Just from a quick glance I can tell you're executing this:

route = [ID1].concat(result);

Many times at the same level. Additional I could not understand your bottom return statements, I feel like there might be an issue there.

about 4 years ago · Juan Pablo Isaza Denunciar
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