mongoose icon indicating copy to clipboard operation
mongoose copied to clipboard

How to update a Number decrease, but not less then 0 ?

Open lawrsp opened this issue 11 years ago • 1 comments

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?

lawrsp avatar Nov 09 '15 18:11 lawrsp

findOneAndUpdate({ total: { $gte: 1 } }, { $inc: { total: -1 } });

vkarpov15 avatar Nov 09 '15 19:11 vkarpov15