Reputation: 98
I have few records in my Mongo DB. The currentStatus field has either of three statuses ("IDLE", "ACTIVE", "SHUTDOWN"). I want to update all records in the currentStatus field from "IDLE" or "ACTIVE" to "SHUTDOWN". I need to make sure they are not in the "SHUTDOWN" state before updating.
The field looks like this:
Tried this but didn't work.
var currentStatusActive = this.collection.find({currentStatus: "ACTIVE"});
var currentStatusIdle = this.collection.find({currentStatus : "IDLE"});
if(currentStatusActive.currentStatus == "ACTIVE" || currentStatusIdle.currentStatus == "IDLE"){
var newStatus = {$set: {currentStatus: "SHUTDOWN"} };
this.collection.updateMany(newStatus, function(err,res){
if (err) throw err;
})
}
Upvotes: 0
Views: 78
Reputation: 1829
You have to wrap the filter conditions and the set target inside the mongodb updateMany function.
try {
db.collection.updateMany(
{"currentStatus": {$in:["ACTIVE","IDLE"]} },
{ $set: { "currentStatus" : "SHUTDOWN" } }
);
} catch (e) {
print(e);
}
See https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/ for more explanation.
Upvotes: 2