'Why is my Update fails to address a field even though it finds the right document?

here is an example of a JSON file of a document

{
  _id: new ObjectId("6212fc135188d701253762a2"),
  title: 'How do i make a lemon?',
  text: '<p>please im confused?????</p>',
  authorUsername: 'SweetWhite',
  dateCreated: 2022-02-21T02:42:27.837Z,
  answers: [
    {
      authorUsername: 'SweetWhite',
      text: '<p>YOU GOTTA DO THE RAIN DANCE YEAH AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA</p>',
      date: 2022-02-21T02:42:42.785Z,
      replies: [],
      likes: [Array],
      dislikes: [Array],
      reports: [Array]
    }
  ],
  likes: [ 0 ],
  dislikes: [ 0 ],
  tag: 'History',
  views: [ 1, 'SweetWhite' ],
  reports: [ 'SweetWhite' ],
  checked: false,
  reportsNested: [ { username: 'SweetWhite', answerIndex: 0 } ],
  __v: 0
}

and here is my query for the update with the router:

router.post('/reportsNested', 
  body('nestedReportObj'),
  body('questionTitle'),

  function(req,res,next){
  console.log(req.body.questionTitle);
  console.log(req.body.nestedReportObj) 
  Question.updateOne({title: req.body.questionTitle},
    {$pull: {reportsNested: req.body.nestedReportObj}},
    function(err, result){
      if(err) {return next(err)}
      console.log(result)  
    }
  )  
  res.redirect(req.get('referer'))
})

the console logs print out

How do i make a lemon?
{"username":"SweetWhite","answerIndex":0}
{
  acknowledged: true,
  modifiedCount: 0,
  upsertedId: null,
  upsertedCount: 0,
  matchedCount: 1
}

the first two console logs print okay values, they find the right document using the title and also they print the right value we want to pull.

from the last log it seems it found the right document, but did not update anything, why is that? in my router I am pulling a value from the reportsNested, which seems to be a field in the JSON file, why is it not pulling anything then? what am I doing wrong?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source