node js -how to delete particular _id data from array












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.










share|improve this question





























    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.










    share|improve this question



























      1












      1








      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.










      share|improve this question
















      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.







      node.js mongodb express






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 6:06







      indu ECE

















      asked Nov 26 '18 at 7:15









      indu ECEindu ECE

      407




      407
























          1 Answer
          1






          active

          oldest

          votes


















          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} }
          });





          share|improve this answer
























          • if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

            – indu ECE
            Nov 26 '18 at 7:58











          • @indu ECE - any errors you are getting?

            – Rahul Raval
            Nov 26 '18 at 8:19











          • That should have worked @induECE

            – Ankit Agarwal
            Nov 26 '18 at 8:19






          • 1





            @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

            – Ankit Agarwal
            Nov 26 '18 at 9:07






          • 1





            sry, i got it. thank u so much

            – indu ECE
            Nov 26 '18 at 9:08












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476285%2fnode-js-how-to-delete-particular-id-data-from-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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} }
          });





          share|improve this answer
























          • if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

            – indu ECE
            Nov 26 '18 at 7:58











          • @indu ECE - any errors you are getting?

            – Rahul Raval
            Nov 26 '18 at 8:19











          • That should have worked @induECE

            – Ankit Agarwal
            Nov 26 '18 at 8:19






          • 1





            @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

            – Ankit Agarwal
            Nov 26 '18 at 9:07






          • 1





            sry, i got it. thank u so much

            – indu ECE
            Nov 26 '18 at 9:08
















          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} }
          });





          share|improve this answer
























          • if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

            – indu ECE
            Nov 26 '18 at 7:58











          • @indu ECE - any errors you are getting?

            – Rahul Raval
            Nov 26 '18 at 8:19











          • That should have worked @induECE

            – Ankit Agarwal
            Nov 26 '18 at 8:19






          • 1





            @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

            – Ankit Agarwal
            Nov 26 '18 at 9:07






          • 1





            sry, i got it. thank u so much

            – indu ECE
            Nov 26 '18 at 9:08














          2












          2








          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} }
          });





          share|improve this answer













          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} }
          });





          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");
          }
          });





          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");
          }
          });






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 7:34









          Ankit AgarwalAnkit Agarwal

          24.4k52245




          24.4k52245













          • if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

            – indu ECE
            Nov 26 '18 at 7:58











          • @indu ECE - any errors you are getting?

            – Rahul Raval
            Nov 26 '18 at 8:19











          • That should have worked @induECE

            – Ankit Agarwal
            Nov 26 '18 at 8:19






          • 1





            @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

            – Ankit Agarwal
            Nov 26 '18 at 9:07






          • 1





            sry, i got it. thank u so much

            – indu ECE
            Nov 26 '18 at 9:08



















          • if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

            – indu ECE
            Nov 26 '18 at 7:58











          • @indu ECE - any errors you are getting?

            – Rahul Raval
            Nov 26 '18 at 8:19











          • That should have worked @induECE

            – Ankit Agarwal
            Nov 26 '18 at 8:19






          • 1





            @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

            – Ankit Agarwal
            Nov 26 '18 at 9:07






          • 1





            sry, i got it. thank u so much

            – indu ECE
            Nov 26 '18 at 9:08

















          if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

          – indu ECE
          Nov 26 '18 at 7:58





          if i use this '$pull: {"answers" : {"_id": req.body.answersId} }' delete function not working

          – indu ECE
          Nov 26 '18 at 7:58













          @indu ECE - any errors you are getting?

          – Rahul Raval
          Nov 26 '18 at 8:19





          @indu ECE - any errors you are getting?

          – Rahul Raval
          Nov 26 '18 at 8:19













          That should have worked @induECE

          – Ankit Agarwal
          Nov 26 '18 at 8:19





          That should have worked @induECE

          – Ankit Agarwal
          Nov 26 '18 at 8:19




          1




          1





          @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

          – Ankit Agarwal
          Nov 26 '18 at 9:07





          @induECE it might be the case that there are no records with ` _id: req.params.id ` or even "_id": req.body.answersId

          – Ankit Agarwal
          Nov 26 '18 at 9:07




          1




          1





          sry, i got it. thank u so much

          – indu ECE
          Nov 26 '18 at 9:08





          sry, i got it. thank u so much

          – indu ECE
          Nov 26 '18 at 9:08




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53476285%2fnode-js-how-to-delete-particular-id-data-from-array%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft