1

this is my delete function route.js

router.delete("/qus/:id/answers/:answersId", function(req, res) {
        Forum.findOneAndUpdate({ _id: req.params.id }, {
                $pull: {
                    "answers" : { "answers._id": req.body.answersId }
                }
            }, { safe: true, multi: true },
            function(err,result) {
                if (err) {
                    console.log(err);
                    res.json({msg:"somethong went wrong"})
                }
                else {
                   res.json({msg:"success"});
                  // res.redirect("/qus/" + foundAnimal._id + "/answers");
                }
            });
    });

my api look like

{
  "success": true,
  "forums": [
    {
      "createdAt": "2018-11-26T05:26:37.829Z",
      "likes": 0,
      "likedBy": [],
      "dislikes": 0,
      "dislikedBy": [],
      "_id": "5bfb84776cd9ea0ebc3db87b",
      "title": "how to apply visa",
      "body": "student visa requirement
      "qus_comments": [],
      "answers": [
        {
          "createdAt": "2018-11-26T06:59:21.840Z",
          "likesAnswer": 0,
          "likedAnswerBy": [],
          "dislikesAnswer": 0,
          "dislikedAnswerBy": [],
          "_id": "5bfb99eb21a1911650eba0d9",
          "content": "asdfghjk",
        },
        {
          "createdAt": "2018-11-26T06:59:21.840Z",
          "likesAnswer": 0,
          "likedAnswerBy": [],
          "dislikesAnswer": 0,
          "dislikedAnswerBy": [],
          "_id": "5bfb99f321a1911650eba0da",
          "content": "refersadfghj",
        }
      ],
      "__v": 13
    }
  ]
}

my delete function working , this function delete whole answers array, but i want delete any particular id in this answers array. please help me. i don't know , how to implement this one . if anyone know please help me.

1 Answer 1

2

You need to use answers._id directly when you are using $pull for the given data structure you have:

Forum.findOneAndUpdate({_id: req.params.id}, {
  $pull: {"answers._id": req.body.answersId}
 },{ safe: true, multi: true },
 function(err,result) {
  if (err) {
      console.log(err);
      res.json({msg:"somethong went wrong"})
  }
  else {
     res.json({msg:"success"});
    // res.redirect("/qus/" + foundAnimal._id + "/answers");
 }
});

In some cases, may be due to difference in mongodb version, below works:

Forum.findOneAndUpdate({_id: req.params.id}, {
  $pull: {"answers" : {"_id": req.body.answersId} }
});
Sign up to request clarification or add additional context in comments.

9 Comments

if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working
@indu ECE - And you shouldn't be using option multi with findOneAndUpdate.
@Ankit Agarwal i didn't get any error, i get success msg ,but that id not delete in api
What about database, is it removed there? @induECE
@induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId
|

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.