Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda