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;
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
add a comment |
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
add a comment |
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
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
javascript mongodb express mongoose
edited Mar 25 at 5:57
Wai Ha Lee
6,296124266
6,296124266
asked Mar 25 at 2:31
FranktheTankFranktheTank
1096
1096
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
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 );
add a comment |
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 );
);
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
add a comment |
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 );
);
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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 );
add a comment |
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 );
add a comment |
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 );
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 );
answered Mar 25 at 2:56
GeraldGerald
1238
1238
add a comment |
add a comment |
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 );
);
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
add a comment |
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 );
);
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
add a comment |
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 );
);
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 );
);
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
add a comment |
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
add a comment |
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 );
);
add a comment |
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 );
);
add a comment |
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 );
);
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 );
);
edited Mar 25 at 7:04
Cody Gray♦
198k37391481
198k37391481
answered Mar 25 at 4:06
user11195629user11195629
112
112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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