Mongoose: is there a way to implement this function by using a single mongoose update operation?












0















I am working on an API call that deletes a user's display picture by seeing weather he has a dp (display picture) or not, a dp is a string (an aws s3 link).



If the user does not have anything in his dp then nothing happens if he has something in his account then the image is deleted and the entry is updated to "".



This is what I tried.



router.post('/deleteDP',jwtValid,function (req,res) {
user.findById({'_id': req.decoded.id}, function (err, result) {
if(err){return res.send("1");} //error
else if(!result){return res.send("2");} //no user found
else if(result)
{
if(result.dp === "")
{
///send deleted
res.send("0");
}else
{
// delete function for result.dp
user.findByIdAndUpdate({'_id':result.id},{$set:{'dp':''}},function (err,result) {
if(result){return res.send("0");}
});
}
}
});
});


Is there a way I can do this using a single mongodb operation rather then two I used in the above code ("findByIdAndUpdate" and "findById")?










share|improve this question





























    0















    I am working on an API call that deletes a user's display picture by seeing weather he has a dp (display picture) or not, a dp is a string (an aws s3 link).



    If the user does not have anything in his dp then nothing happens if he has something in his account then the image is deleted and the entry is updated to "".



    This is what I tried.



    router.post('/deleteDP',jwtValid,function (req,res) {
    user.findById({'_id': req.decoded.id}, function (err, result) {
    if(err){return res.send("1");} //error
    else if(!result){return res.send("2");} //no user found
    else if(result)
    {
    if(result.dp === "")
    {
    ///send deleted
    res.send("0");
    }else
    {
    // delete function for result.dp
    user.findByIdAndUpdate({'_id':result.id},{$set:{'dp':''}},function (err,result) {
    if(result){return res.send("0");}
    });
    }
    }
    });
    });


    Is there a way I can do this using a single mongodb operation rather then two I used in the above code ("findByIdAndUpdate" and "findById")?










    share|improve this question



























      0












      0








      0








      I am working on an API call that deletes a user's display picture by seeing weather he has a dp (display picture) or not, a dp is a string (an aws s3 link).



      If the user does not have anything in his dp then nothing happens if he has something in his account then the image is deleted and the entry is updated to "".



      This is what I tried.



      router.post('/deleteDP',jwtValid,function (req,res) {
      user.findById({'_id': req.decoded.id}, function (err, result) {
      if(err){return res.send("1");} //error
      else if(!result){return res.send("2");} //no user found
      else if(result)
      {
      if(result.dp === "")
      {
      ///send deleted
      res.send("0");
      }else
      {
      // delete function for result.dp
      user.findByIdAndUpdate({'_id':result.id},{$set:{'dp':''}},function (err,result) {
      if(result){return res.send("0");}
      });
      }
      }
      });
      });


      Is there a way I can do this using a single mongodb operation rather then two I used in the above code ("findByIdAndUpdate" and "findById")?










      share|improve this question
















      I am working on an API call that deletes a user's display picture by seeing weather he has a dp (display picture) or not, a dp is a string (an aws s3 link).



      If the user does not have anything in his dp then nothing happens if he has something in his account then the image is deleted and the entry is updated to "".



      This is what I tried.



      router.post('/deleteDP',jwtValid,function (req,res) {
      user.findById({'_id': req.decoded.id}, function (err, result) {
      if(err){return res.send("1");} //error
      else if(!result){return res.send("2");} //no user found
      else if(result)
      {
      if(result.dp === "")
      {
      ///send deleted
      res.send("0");
      }else
      {
      // delete function for result.dp
      user.findByIdAndUpdate({'_id':result.id},{$set:{'dp':''}},function (err,result) {
      if(result){return res.send("0");}
      });
      }
      }
      });
      });


      Is there a way I can do this using a single mongodb operation rather then two I used in the above code ("findByIdAndUpdate" and "findById")?







      node.js mongodb mongoose mongodb-query






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 11:54







      Sonic_

















      asked Jan 19 at 11:41









      Sonic_Sonic_

      3617




      3617
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Yes there is a way:



          router.post('/deleteDP',jwtValid,function (req,res) {
          user.findOneAndUpdate({$and:[{'_id': req.decoded.id},{db:{$exists:true}}]},{$set:{'dp':''}}, function (err, result) {
          if(!err){
          if(!result){
          return res.send("2")
          }
          res.send("0");
          }
          res.send("1")

          });

          });


          This should work :);






          share|improve this answer


























          • Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

            – Sonic_
            Jan 19 at 16:56













          • I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

            – vitomadio
            Jan 19 at 17:50











          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%2f54266698%2fmongoose-is-there-a-way-to-implement-this-function-by-using-a-single-mongoose-u%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









          0














          Yes there is a way:



          router.post('/deleteDP',jwtValid,function (req,res) {
          user.findOneAndUpdate({$and:[{'_id': req.decoded.id},{db:{$exists:true}}]},{$set:{'dp':''}}, function (err, result) {
          if(!err){
          if(!result){
          return res.send("2")
          }
          res.send("0");
          }
          res.send("1")

          });

          });


          This should work :);






          share|improve this answer


























          • Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

            – Sonic_
            Jan 19 at 16:56













          • I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

            – vitomadio
            Jan 19 at 17:50
















          0














          Yes there is a way:



          router.post('/deleteDP',jwtValid,function (req,res) {
          user.findOneAndUpdate({$and:[{'_id': req.decoded.id},{db:{$exists:true}}]},{$set:{'dp':''}}, function (err, result) {
          if(!err){
          if(!result){
          return res.send("2")
          }
          res.send("0");
          }
          res.send("1")

          });

          });


          This should work :);






          share|improve this answer


























          • Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

            – Sonic_
            Jan 19 at 16:56













          • I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

            – vitomadio
            Jan 19 at 17:50














          0












          0








          0







          Yes there is a way:



          router.post('/deleteDP',jwtValid,function (req,res) {
          user.findOneAndUpdate({$and:[{'_id': req.decoded.id},{db:{$exists:true}}]},{$set:{'dp':''}}, function (err, result) {
          if(!err){
          if(!result){
          return res.send("2")
          }
          res.send("0");
          }
          res.send("1")

          });

          });


          This should work :);






          share|improve this answer















          Yes there is a way:



          router.post('/deleteDP',jwtValid,function (req,res) {
          user.findOneAndUpdate({$and:[{'_id': req.decoded.id},{db:{$exists:true}}]},{$set:{'dp':''}}, function (err, result) {
          if(!err){
          if(!result){
          return res.send("2")
          }
          res.send("0");
          }
          res.send("1")

          });

          });


          This should work :);







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 19 at 17:48

























          answered Jan 19 at 12:21









          vitomadiovitomadio

          560110




          560110













          • Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

            – Sonic_
            Jan 19 at 16:56













          • I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

            – vitomadio
            Jan 19 at 17:50



















          • Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

            – Sonic_
            Jan 19 at 16:56













          • I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

            – vitomadio
            Jan 19 at 17:50

















          Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

          – Sonic_
          Jan 19 at 16:56







          Hey @vitomadio This would set the dp:'' always, I wanted to check if the user's dp is '' or not, how do I do this in a single operation?.....Only if it is not '', then I wanted to make it '' .

          – Sonic_
          Jan 19 at 16:56















          I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

          – vitomadio
          Jan 19 at 17:50





          I just edit the code, I not a 100% sure it works but could give you an idea. Happy coding.

          – vitomadio
          Jan 19 at 17:50


















          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%2f54266698%2fmongoose-is-there-a-way-to-implement-this-function-by-using-a-single-mongoose-u%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

          Homophylophilia

          Updating UILabel text programmatically using a function

          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage