0

I am trying to push an object to the deeply nested checklist array below :

{
 // User
  _id : "id",
  days : [
     {
       _id : "id"
       day : "6",
       tasks: [
          {
            _id : "id",
            name : "todo"
            types : {
              checklist : [] // i want to push objects here
              notes : []
            }
          },
          ....
       ]
     },
     ....
  ]
}

Here is what i have tried but failed... my guess is that the $ operator shouldn't be at that position:

User.update({_id : user._id , "days.tasks.name": 'todo' } ,{ $push : { 'days.tasks.$.types.checklist' : { data : 'hello World' } }})
1

2 Answers 2

0

Or you can re-design your DB and made some child Model, ref with ObjectId.

Sign up to request clarification or add additional context in comments.

Comments

0

I found the solution which basically looking for the index of the specific task using the unique id:

router.patch('/user/updatetask', auth , async (req,res)=>{
    const user = req.user
    const dayIndex = user.days.indexOf(user.days.find(day => day.day == req.body.day))
    const taskIndex = user.days[dayIndex].tasks.indexOf(user.days[dayIndex].tasks.find(task => task._id == req.body.task_id))
    var obj = {}
        obj[`days.${dayIndex}.tasks.${taskIndex}.types.checklist`] = 'no'
    try {

        await  User.updateOne({_id : user._id } ,{ $push :obj})
        res.status(201).send(user)
    } catch(e) {
        res.status(400).send(e)
    }
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.