singleObj = await Objects.findByIdAndUpdate({ _id: req.body.id, }, { $inc: { 'total_obj': -1, 'total_stuff': 1 }, }, { new: true })
Whenever the user clicks a button, we decrement the value of 'total_obj' by one. It's important to note that the value can be zero or greater. I attempted to achieve this using the following code:
singleObj = await Objects.findByIdAndUpdate(
{ _id: req.body.id, "total_obj": { "$lt": 0 } },
{ "$set": { "total_obj": 0 } }
);
Unfortunately, this approach causes issues every time the page is loaded and sets the values to 0. I also included the following definition in the schema:
total_obj: {
type: Number,
required: true,
min: 0
},