Home > Back-end >  How to find value in array of object in mongodb
How to find value in array of object in mongodb

Time:06-05

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 }

CodePudding user response:

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

As you tasks is an array

CodePudding user response:

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.

CodePudding user response:

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

  • Related