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

77
Vistas
Get a flat array of deeply nested items in MongoDB

I am building a side project that uses a mongodb database. I am using the MERN stack to build out a web application with my data. I am using Mongoose to get data from my database on a NodeJS/Express server to build my REST API. Here is an example of the data I have. I have deeply nested subdocuments that I need to retrieve. Each user has an array of 'contacts'. Each 'contact' has an array of 'tasks'. I would like to get an array of all tasks that are nested in each user. For example, if a user has 2 contacts, and each contact has 3 tasks, I would like an array with 6 tasks.

{
   userName: 'jdoe',
   uid: 12343,
   contacts: [
      {
          name: 'Jane Doe',
          tasks:[
             {
                taskName: 'Task1'
             },
             {
                taskName: 'Task2'
             },
             {
                taskName: 'Task3'
             },
          
          ]
      },
      {
          name: 'Bob Smith',
          tasks:[
             {
                taskName: 'TaskA'
             },
             {
                taskName: 'TaskB'
             },
             {
                taskName: 'TaskC'
             },
          
          ]
      }
   ]
}

To query my data with Mongoose, I tried something like this

User.findOne({uid: req.params.uid}, 'contacts.tasks', (yasks,err)=>{
  if(err){
    res.send(err)
  }else{
      res.send(tasks)
    }
})

However, this query returns something like this

{
"_id": "353453",
"contacts": [
    {
        "tasks": [
            {
                 taskName: 'Task1'
            }
        ]
    },
    {
        "tasks": [
            {
                 taskName: 'Task1'
            },
            {
                 taskName: 'Task1'
            }
        ]
    },
    {
        "tasks": []
    },
    {
        "tasks": []
    }
   ]
}

I am looking for a response such as this

       [
            {
                 taskName: 'Task1'
            },
            {
                 taskName: 'Task1'
            },
                 taskName: 'Task1'
            },
            {
                 taskName: 'Task1'
            }
        ]  

I am wondering if there is any way I can do this with Mongoose. If not, what kind of javascript can I run to transform my data to be structured like this?

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

0

You can use $reduce for this:

User.aggregate([
  {$match: {uid: req.params.uid}},
  {
    $project: {
      tasks: {
        $reduce: {
          input: "$contacts",
          initialValue: [],
          in: {$concatArrays: ["$$value", "$$this.tasks"]}
        }
      },
      _id: 0
    }
  }
])

See how it works on the playground example

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