Is there a way in mongodb to use if/else to set a field value during an update. i know that i can use find, to return documents, loop over them, and do if/else check on each and make a new save query for each of the documents.
However, that seems wasteful, if there is a way to update conditionally in one go.
Is it possible to conditionally set a field value, e.g like this
Documents.update(
{some_condition: true},
{$set: {"status":
{$cond:
{if : {"some field": "some condition"}},
{then: "value 1"} ,
{else: "value 2"}
}
}}
)
(I know that $cond is used for aggregation, i used it here as an example of what i have in mind.)
Is there a way in mongodb to use if/else to set a field value during an update. i know that i can use find, to return documents, loop over them, and do if/else check on each and make a new save query for each of the documents.
However, that seems wasteful, if there is a way to update conditionally in one go.
Is it possible to conditionally set a field value, e.g like this
Documents.update(
{some_condition: true},
{$set: {"status":
{$cond:
{if : {"some field": "some condition"}},
{then: "value 1"} ,
{else: "value 2"}
}
}}
)
(I know that $cond is used for aggregation, i used it here as an example of what i have in mind.)
Share Improve this question edited Apr 27, 2022 at 14:01 Rainer Plumer asked May 21, 2015 at 22:53 Rainer PlumerRainer Plumer 3,7533 gold badges28 silver badges42 bronze badges 2- why wouldnt you put that logic in your application code? If your determine to run database side you can use a saved function (although not recommended) docs.mongodb.org/manual/tutorial/… – Rob Commented May 21, 2015 at 23:07
- 2 @Rob I checked out the saved function approach ,and it seems to have too many negative sides for me to use it. The logic would basically remain in the application, its just one conditional statement. I want to avoid looping over the returned documents, and saving each of those separately. If there is a way to update conditionally, then i could update lots of documents in one query. – Rainer Plumer Commented May 21, 2015 at 23:29
2 Answers
Reset to default 8MongoDB doesn't support the sort of conditional update you're looking for. However, you can still do better than using a find, loop, and save approach.
Move the condition check into the update
query selector and then issue two updates (one for each case), using {multi: true}
to apply the update to all matched docs.
// Start with the "if" update
Documents.update(
{some_condition: true, "some field": "some condition"},
{$set: {"status": "value 1"}},
{multi: true},
function(err, numAffected) {
// Now do the "else" update, using $ne to select the rest of the docs
Documents.update(
{some_condition: true, "some field": {$ne: "some condition"}},
{$set: {"status": "value 2"}},
{multi: true},
function(err, numAffected) {
// All done.
}
)
}
)
Answer: Yes
It's pretty much possible using the $set stage in the aggregation pipeline
- Use $set to compute the conditional value in each record
- Use $out to update the records in the collection
Example:
db.collectionname.aggregate(
[
{
$set: { //Compute the new value
'ResultField': {
$cond: {
if: {
$lt: ['$ScoreField', 40]
},
then: "Fail",
else: "Pass"
}
}
}
},
{
$out: 'collectionname' //Update the records in collection
}
]
)