我会尽力描述我的问题。
我用 Mongoose 创建了一个 Schema,试图设置这样的最大值:
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 到我的“mana”变量:
const manaAdd = async () => await inventoryModel.updateMany({},
{
$inc: {
mana: 1
}
}
);
setInterval(manaAdd, 30000);
但无论如何我的法力变量最终超过了100... 如果你能帮到我,我将不胜感激
ZartaX0O3
答案 0 :(得分:0)
来自猫鼬文档
<块引用>在上面的示例中,您了解了文档验证。 Mongoose 还支持对 update()、updateOne()、updateMany() 和 findOneAndUpdate() 操作的验证。更新验证器默认关闭 - 您需要指定 runValidators 选项。
所以你必须像下面的代码一样将 runValidater 设置为 true
const opts = { runValidators: true };
Toy.updateOne({}, { color: 'not a color' }, opts, function(err) {
assert.equal(err.errors.color.message,
'Invalid color');
});
答案 1 :(得分:0)
这里的代码没有任何变化
const opts = { runValidators: true };
const manaAdd = async () => await inventoryModel.updateMany({}, { $inc: {mana: 1 }}, opts);
setInterval(manaAdd, 30000);