I have mongoose schema with field validate option min:
export const CartSchema = new Schema<ICartSchema>({
amount: {
type: Number,
default: 1,
min: 1
}
}, { versionKey: false });
when i try update my document and set value <1, i didnt get some error and document get value <1
switch (type) {
case "inc": {
update = { $inc: { amount: 1 } }
break;
}
case "dec": {
update = { $inc: { amount: -1 } }
break;
}
default: throw Error("Bad request");
}
const cart = await Cart.findOneAndUpdate({ _id: cartId }, update,
{
new: true,
runValidators: true
}).populate({
path: "product",
select: {
user: 0
}
});
mongoose version ^6.0.7
CodePudding user response:
As docs says:
One final detail worth noting: update validators only run on the following update operators:
$set$unset$push(>= 4.8.0)$addToSet(>= 4.8.0)$pull(>= 4.12.0)$pullAll(>= 4.12.0)
Even the uses $inc as example where validators doesn't work:
For instance, the below update will succeed, regardless of the value of number, because update validators ignore $inc.
