Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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 }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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

As your tasks is an array

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!