Mongoose/MongoDb FindOneAndUpdate not working as expectedHow do JavaScript closures work?How does JavaScript .prototype work?MongoDB vs. CassandraHow to query MongoDB with “like”?How do I drop a MongoDB database from the command line?How does data binding work in AngularJS?Find MongoDB records where array field is not emptyMongoose: findOneAndUpdate doesn't return updated documentMongoose findOneAndUpdate not returning newmongoose update many don't work with $cond in node.js

Why is gun control associated with the socially liberal Democratic party?

Someone who is granted access to information but not expected to read it

How can this shape perfectly cover a cube?

Is there a risk to write an invitation letter for a stranger to obtain a Czech (Schengen) visa?

Jam with honey & without pectin has a saucy consistency always

What should I be aware of in buying second-hand sinks and toilets?

Can artificial satellite positions affect tides?

Struggling to present results from long papers in short time slots

Can an opamp have its own voltage regulator?

Is it possible to install Firefox on Ubuntu with no desktop enviroment?

What is the color associated with lukewarm?

How did the European Union reach the figure of 3% as a maximum allowed deficit?

Can an escape pod land on Earth from orbit and not be immediately detected?

Why is Skinner so awkward in Hot Fuzz?

Skills with different abilities: How to adjudicate what combination to use?

Where can the Tosafot of Rabbi Samson of Sens be found?

How to make a villain when your PCs are villains?

Sakkāya-Ditthi and Self-View

Does WiFi affect the quality of images downloaded from the internet?

Can an open source licence be revoked if it violates employer's IP?

100-doors puzzle

How to search for Android apps without ads?

Change Data Capture and Async Triggers

Was the Lonely Mountain, where Smaug lived, a volcano?



Mongoose/MongoDb FindOneAndUpdate not working as expected


How do JavaScript closures work?How does JavaScript .prototype work?MongoDB vs. CassandraHow to query MongoDB with “like”?How do I drop a MongoDB database from the command line?How does data binding work in AngularJS?Find MongoDB records where array field is not emptyMongoose: findOneAndUpdate doesn't return updated documentMongoose findOneAndUpdate not returning newmongoose update many don't work with $cond in node.js






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








3















I am trying to update a certain location only if its a status: 0 or status 2. Do not update if status is 1. I only have one copy of that location.



Property.findOneAndUpdate( status: 0, location: req.body.update.location , req.body.update, err => 
if (err) return res.json( success: false, error: err );
return res.json( success: true );
);
Property.findOneAndUpdate( status: 2, location: req.body.update.location , req.body.update, err =>
if (err) return res.json( success: false, error: err );
return res.json( success: true );
);


However, the above code is updating the property even when the status is 1.



Property.find(location: req.body.update.location, (err, Propertyz) => 
myProperty = Propertyz
console.log(myProperty[0].status)
if(myProperty[0].status != 1) // returns and doesn't update if true
console.log("updating")
Property.findOneAndUpdate( location: req.body.update.location , req.body.update, err =>
if (err) return res.json( success: false, error: err );
return res.json( success: true );
);

else
console.log("rejecting")
return res.json( success: true );

)


I changed it to this and it works, but I don't understand why the previous was not working or if there was a way to condense the two previous functions into one.










share|improve this question






























    3















    I am trying to update a certain location only if its a status: 0 or status 2. Do not update if status is 1. I only have one copy of that location.



    Property.findOneAndUpdate( status: 0, location: req.body.update.location , req.body.update, err => 
    if (err) return res.json( success: false, error: err );
    return res.json( success: true );
    );
    Property.findOneAndUpdate( status: 2, location: req.body.update.location , req.body.update, err =>
    if (err) return res.json( success: false, error: err );
    return res.json( success: true );
    );


    However, the above code is updating the property even when the status is 1.



    Property.find(location: req.body.update.location, (err, Propertyz) => 
    myProperty = Propertyz
    console.log(myProperty[0].status)
    if(myProperty[0].status != 1) // returns and doesn't update if true
    console.log("updating")
    Property.findOneAndUpdate( location: req.body.update.location , req.body.update, err =>
    if (err) return res.json( success: false, error: err );
    return res.json( success: true );
    );

    else
    console.log("rejecting")
    return res.json( success: true );

    )


    I changed it to this and it works, but I don't understand why the previous was not working or if there was a way to condense the two previous functions into one.










    share|improve this question


























      3












      3








      3








      I am trying to update a certain location only if its a status: 0 or status 2. Do not update if status is 1. I only have one copy of that location.



      Property.findOneAndUpdate( status: 0, location: req.body.update.location , req.body.update, err => 
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );
      Property.findOneAndUpdate( status: 2, location: req.body.update.location , req.body.update, err =>
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );


      However, the above code is updating the property even when the status is 1.



      Property.find(location: req.body.update.location, (err, Propertyz) => 
      myProperty = Propertyz
      console.log(myProperty[0].status)
      if(myProperty[0].status != 1) // returns and doesn't update if true
      console.log("updating")
      Property.findOneAndUpdate( location: req.body.update.location , req.body.update, err =>
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );

      else
      console.log("rejecting")
      return res.json( success: true );

      )


      I changed it to this and it works, but I don't understand why the previous was not working or if there was a way to condense the two previous functions into one.










      share|improve this question
















      I am trying to update a certain location only if its a status: 0 or status 2. Do not update if status is 1. I only have one copy of that location.



      Property.findOneAndUpdate( status: 0, location: req.body.update.location , req.body.update, err => 
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );
      Property.findOneAndUpdate( status: 2, location: req.body.update.location , req.body.update, err =>
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );


      However, the above code is updating the property even when the status is 1.



      Property.find(location: req.body.update.location, (err, Propertyz) => 
      myProperty = Propertyz
      console.log(myProperty[0].status)
      if(myProperty[0].status != 1) // returns and doesn't update if true
      console.log("updating")
      Property.findOneAndUpdate( location: req.body.update.location , req.body.update, err =>
      if (err) return res.json( success: false, error: err );
      return res.json( success: true );
      );

      else
      console.log("rejecting")
      return res.json( success: true );

      )


      I changed it to this and it works, but I don't understand why the previous was not working or if there was a way to condense the two previous functions into one.







      javascript mongodb express mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 5:57









      Wai Ha Lee

      6,296124266




      6,296124266










      asked Mar 25 at 2:31









      FranktheTankFranktheTank

      1096




      1096






















          3 Answers
          3






          active

          oldest

          votes


















          1














          You can update it using one findOneAndUpdate function, add $or operator on your query. https://docs.mongodb.com/manual/reference/operator/query/or/. This code below search documents that has status 2 or 0.



          Property.findOneAndUpdate( $or: [status: 2, status: 0], location: req.body.update.location , req.body.update, err => {
          if (err) return res.json( success: false, error: err );
          return res.json( success: true );





          share|improve this answer






























            2














             Property.findOneAndUpdate( $or: [ status: 0, status: 2 ] , location: req.body.update.location , err => 
            if (err) return res.json( success: false, error: err );
            return res.json( success: true );
            );





            share|improve this answer


















            • 1





              Some comments to explain your code would really help others understand what you've done. From review.

              – Wai Ha Lee
              Mar 25 at 5:57


















            1














            Property.findOneAndUpdate( status: 0, location: req.body.update.location , $set:req.body.update , err => 
            if (err) return res.json( success: false, error: err );
            return res.json( success: true );
            );
            Property.findOneAndUpdate( status: 2, location: req.body.update.location , $set:req.body.update , err =>
            if (err) return res.json( success: false, error: err );
            return res.json( success: true );
            );





            share|improve this answer

























              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%2f55330557%2fmongoose-mongodb-findoneandupdate-not-working-as-expected%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              You can update it using one findOneAndUpdate function, add $or operator on your query. https://docs.mongodb.com/manual/reference/operator/query/or/. This code below search documents that has status 2 or 0.



              Property.findOneAndUpdate( $or: [status: 2, status: 0], location: req.body.update.location , req.body.update, err => {
              if (err) return res.json( success: false, error: err );
              return res.json( success: true );





              share|improve this answer



























                1














                You can update it using one findOneAndUpdate function, add $or operator on your query. https://docs.mongodb.com/manual/reference/operator/query/or/. This code below search documents that has status 2 or 0.



                Property.findOneAndUpdate( $or: [status: 2, status: 0], location: req.body.update.location , req.body.update, err => {
                if (err) return res.json( success: false, error: err );
                return res.json( success: true );





                share|improve this answer

























                  1












                  1








                  1







                  You can update it using one findOneAndUpdate function, add $or operator on your query. https://docs.mongodb.com/manual/reference/operator/query/or/. This code below search documents that has status 2 or 0.



                  Property.findOneAndUpdate( $or: [status: 2, status: 0], location: req.body.update.location , req.body.update, err => {
                  if (err) return res.json( success: false, error: err );
                  return res.json( success: true );





                  share|improve this answer













                  You can update it using one findOneAndUpdate function, add $or operator on your query. https://docs.mongodb.com/manual/reference/operator/query/or/. This code below search documents that has status 2 or 0.



                  Property.findOneAndUpdate( $or: [status: 2, status: 0], location: req.body.update.location , req.body.update, err => {
                  if (err) return res.json( success: false, error: err );
                  return res.json( success: true );






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 2:56









                  GeraldGerald

                  1238




                  1238























                      2














                       Property.findOneAndUpdate( $or: [ status: 0, status: 2 ] , location: req.body.update.location , err => 
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );





                      share|improve this answer


















                      • 1





                        Some comments to explain your code would really help others understand what you've done. From review.

                        – Wai Ha Lee
                        Mar 25 at 5:57















                      2














                       Property.findOneAndUpdate( $or: [ status: 0, status: 2 ] , location: req.body.update.location , err => 
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );





                      share|improve this answer


















                      • 1





                        Some comments to explain your code would really help others understand what you've done. From review.

                        – Wai Ha Lee
                        Mar 25 at 5:57













                      2












                      2








                      2







                       Property.findOneAndUpdate( $or: [ status: 0, status: 2 ] , location: req.body.update.location , err => 
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );





                      share|improve this answer













                       Property.findOneAndUpdate( $or: [ status: 0, status: 2 ] , location: req.body.update.location , err => 
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Mar 25 at 4:02









                      Prachi JainPrachi Jain

                      414




                      414







                      • 1





                        Some comments to explain your code would really help others understand what you've done. From review.

                        – Wai Ha Lee
                        Mar 25 at 5:57












                      • 1





                        Some comments to explain your code would really help others understand what you've done. From review.

                        – Wai Ha Lee
                        Mar 25 at 5:57







                      1




                      1





                      Some comments to explain your code would really help others understand what you've done. From review.

                      – Wai Ha Lee
                      Mar 25 at 5:57





                      Some comments to explain your code would really help others understand what you've done. From review.

                      – Wai Ha Lee
                      Mar 25 at 5:57











                      1














                      Property.findOneAndUpdate( status: 0, location: req.body.update.location , $set:req.body.update , err => 
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );
                      Property.findOneAndUpdate( status: 2, location: req.body.update.location , $set:req.body.update , err =>
                      if (err) return res.json( success: false, error: err );
                      return res.json( success: true );
                      );





                      share|improve this answer





























                        1














                        Property.findOneAndUpdate( status: 0, location: req.body.update.location , $set:req.body.update , err => 
                        if (err) return res.json( success: false, error: err );
                        return res.json( success: true );
                        );
                        Property.findOneAndUpdate( status: 2, location: req.body.update.location , $set:req.body.update , err =>
                        if (err) return res.json( success: false, error: err );
                        return res.json( success: true );
                        );





                        share|improve this answer



























                          1












                          1








                          1







                          Property.findOneAndUpdate( status: 0, location: req.body.update.location , $set:req.body.update , err => 
                          if (err) return res.json( success: false, error: err );
                          return res.json( success: true );
                          );
                          Property.findOneAndUpdate( status: 2, location: req.body.update.location , $set:req.body.update , err =>
                          if (err) return res.json( success: false, error: err );
                          return res.json( success: true );
                          );





                          share|improve this answer















                          Property.findOneAndUpdate( status: 0, location: req.body.update.location , $set:req.body.update , err => 
                          if (err) return res.json( success: false, error: err );
                          return res.json( success: true );
                          );
                          Property.findOneAndUpdate( status: 2, location: req.body.update.location , $set:req.body.update , err =>
                          if (err) return res.json( success: false, error: err );
                          return res.json( success: true );
                          );






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Mar 25 at 7:04









                          Cody Gray

                          198k37391481




                          198k37391481










                          answered Mar 25 at 4:06









                          user11195629user11195629

                          112




                          112



























                              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%2f55330557%2fmongoose-mongodb-findoneandupdate-not-working-as-expected%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

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                              155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해