Would mongodb provide a new update operator to make compare-and-set updates? Something like:
{ "$cond" : { "foo": [ oldValue, newValue ] } }
mongodb set `newValue` to the "foo" field only when the original value of "foo" is equal to `oldValue`.
Further more, the `oldValue` could be a boolean expression with the original value, and mongodb updates if the condition is true:
// if (doc.foo != 1) { doc.foo = 2; } { "$cond" : { "foo": [ {"$ne": 1}, 2 ] } }
There are already two similar operators: $min and $max, so I think this enhancement should be possible as well.
- related to
-
SERVER-6566 Support conditional updates: $updates
- Closed
-
SERVER-14609 Add $setOnMissing update operator
- Closed