'MongoDB - findOneAndUpdate() update using $set and $cond on single field

I am trying to update a field (statusObject.machineA.status) to 'working' in a database only if its value is 'waiting'. I have tried the following:

Project.findOneAndUpdate({
        $or: [
            {"statusObject.machineA.status":"waiting"},
            {"statusObject.machineB.status":"waiting"}
        ]
    }, [{
          $set: {
            "statusObject.machineA.status": {
              $cond: [
                { $eq: ["$statusObject.machineA.status", "waiting"] },
                "working",
                "waiting"
              ]
            }
          }
       }], {
        new: true
    }, function(err, document) {});

My database looks like this:

var statusObject = {
        machineA:{
                status: 'waiting',
                buildProgress: 0
        },
        machineB:{
                status: 'waiting',
                buildProgress: 0
        }
};

For Example, I want the below JSON:

statusObject: {
        machineA:{
                status: 'waiting',
                buildProgress: 0
        },
        machineB:{
                status: 'waiting',
                buildProgress: 0
        }
}

to look like below after executing the query:

statusObject:{
        machineA:{
                status: 'working',
                buildProgress: 0
        },
        machineB:{
                status: 'waiting',
                buildProgress: 0
        }
}

OR (Not Both)

statusObject:{
        machineA:{
                status: 'waiting',
                buildProgress: 0
        },
        machineB:{
                status: 'working',
                buildProgress: 0
        }
}

But after a little testing, I realized that the $cond object does not accept the field used in the $set i.e statusObject.machineA.status. That is, when I update one field based on another field's value, it works, but it doesn't work if I update a field based on its own value. No errors are shown, but the query just doesn't update the field.

Is there something wrong syntax-wise for the nested object or is it just not supported? Any alternative method/s will be appreciated. Thanks!



Solution 1:[1]

Turns out, aggregation pipelines are not supported for mongoose 5.xxx. I got it working by just updating my Mongoose to the latest one:

npm update mongoose

Thanks @Takis _

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nephilimDarth