我会尽力描述我的问题。
我用Mongoose创建了一个模式,试图将最大值设置为如下所示:
const mongoose = require("mongoose");
const inventorySchema = new mongoose.Schema({
userID: { type: String, require: true, unique: true},
mana: { type: Number, default: 0, max: 100,min: 0},
pickaxe: { type: Number, default: 0},
sword: { type: Number, default: 0 },
stone: { type: Number, default: 0},
iron: { type: Number, default: 0},
gold: { type: Number, default: 0},
});
const model = mongoose.model("InventoryModels", inventorySchema);
module.exports = model;
我想做的第二件事是每60秒增加1到我的“法力值”变量,代码如下:
const manaAdd = async () => await inventoryModel.updateMany({},
{
$inc: {
mana: 1
}
}
);
setInterval(manaAdd, 30000);
但无论如何我的法力变数最终超过了100...如果你能帮忙,我将不胜感激
Zartax0O3
来自Mongoose文档
在上面的示例中,您了解了文档验证。Mongoose还支持对update()、updateOne()、updateMany()和findOneAndUpdate()操作的验证。默认情况下Update validators是关闭的-您需要指定runValidators选项。
所以您必须像前面的代码一样将runValidater设置为true
const opts = { runValidators: true };
Toy.updateOne({}, { color: 'not a color' }, opts, function(err) {
assert.equal(err.errors.color.message,
'Invalid color');
});