I want to update a field in my MongoDB database with Mongoose if the new value of that field wouldn't be greater than another field. Here is my code:
Training.findOneAndUpdate(
{ date: dateStringForDB, startHour: retrievedRequest.body.trainingStartHour },
{ $push: { participants: retrievedRequest.session.loggedInPhoneNumber }, $inc: { currentQuota: +1 } },
(err, doc, updateResponse) => {
if (err) {
console.log("Error while updating training ", err);
return;
}
retrievedResponse.redirect("/schedule");
}
);
What I want to do is increment the value of currentQuota
only if value of 'currentQuota' is smaller than another field's (initialQuota
) value. Is there a way to do it with findOneAndUpdate
method or shall I call find
method first and then update
?
Thanks in advance!