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

159
Vistas
How to find value in array of object in mongodb

I want to get the value of a nested object within an array. I get the _id of the parent object, as well as the _id of the child object within the array, but I am not able to retrieve the value of "solvedOn" to toggle a checkbox (behavior).

So this is an example object with its nested objects:

{
  _id: new ObjectId("6292722ea8173377658171ff"),
  title: 'work-list',
  tasks: [
    {
      name: 'go to work',
      createdOn: '2022-05-28T19:04:14.799Z',
      solvedOn: false,
      _id: new ObjectId("6292722ea8173377658171f8")
    },
    {
      name: 'do some work',
      createdOn: '2022-05-30T17:20:56.272Z',
      solvedOn: false,
      _id: new ObjectId("6294fdbf8717c09237c5098e")
    }
  ],
  __v: 0
}

So I already went this way:

  List.findOne(
    {_id: listID},
    {tasks: {
      $elemMatch: {
        _id: taskID
      }
    }},
    (err, res) => {
      console.log(res.tasks);
    }
  )

When I try to log "res.tasks.solvedOn" then the result is 'undefined'. Even though, when I use the placeholder: "res.tasks.$.solvedOn"

This is what I get back:

[
  {
    name: 'go to work',
    createdOn: '2022-05-28T19:04:14.799Z',
    solvedOn: false,
    _id: new ObjectId("6292722ea8173377658171f8")
  }
]

So how I can work with the value of "solvedOn" to toggle the status of a checkbox?

Like:

If (true) { // set value to false } else {// set value to true }
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

You need to write res.tasks[0].solvedOn instead of res.tasks.solvedOn

As your tasks is an array

over 4 years ago · Santiago Trujillo Denunciar

0

When you write res.tasks, it returns an array of tasks. So you cannot get the solvedOn value with res.tasks.solvedOn. You should give the index of the array to get the value of it. Like res.tasks[0].solvedOn.

My suggestion is first you get all the tasks in an array like:

const tasks = res.tasks;

Then map it wherever you want.

over 4 years ago · Santiago Trujillo Denunciar

0

If the item you need may not be the first one, you can use an aggregation pipeline with a $filter:

db.collection.aggregate([
  {
    $match: {_id: ObjectId("6292722ea8173377658171ff")}
  },
  {
    $project: {
      _id: 0,
      res: {
        $filter: {
          input: "$tasks",
          as: "item",
          cond: {$eq: ["$$item._id", ObjectId("6294fdbf8717c09237c5098e")]}
        }
      }
    }
  }
])

playground example

over 4 years ago · Santiago Trujillo 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