mongoose
mongoose copied to clipboard
How to update a Number decrease, but not less then 0 ?
I wonder if this can be done by a single call like findAndUpdate
and also set other fields rightly
eg:
counter = Schema ({ updated_at: Date, total: Number, });
Counter.findOneAndUpdate({}, {$inc: {total: -1}, {$currentDate: {updated_at: true}}, function (err, raw) {});
but absolutely this cannot make true 'total' always equal or greater than 0
Is there a way?
findOneAndUpdate({ total: { $gte: 1 } }, { $inc: { total: -1 } });