MongooseJS$inc在尝试增量时不工作
我正在制作一个discord机器人来进行调节,我需要在我的数据库中存储惩罚的数量,其工作原理如下:每次你警告某人时,TotalPremissions计数器应该和warnings计数器一样增加1。通过谷歌搜索,我发现了关于$inc的声明。我试过使用它,警告计数器的增量很好。但是总计数器一点也没有改变 这是我的密码:MongooseJS$inc在尝试增量时不工作,mongoose,Mongoose,我正在制作一个discord机器人来进行调节,我需要在我的数据库中存储惩罚的数量,其工作原理如下:每次你警告某人时,TotalPremissions计数器应该和warnings计数器一样增加1。通过谷歌搜索,我发现了关于$inc的声明。我试过使用它,警告计数器的增量很好。但是总计数器一点也没有改变 这是我的密码: await mongo().then(async mongoose => { try { const userInfo = await p
await mongo().then(async mongoose => {
try {
const userInfo = await punishmentSchema.findOne({_id: guild.id, userId: member.id});
if(userInfo) {
await punishmentSchema.findOneAndUpdate({
_id: guild.id,
userId: member.id
}, {
$inc: {'totalPunishments': 1},
$inc: {'warnings': 1},
lastPunishment: timestamp('YYYYMMDDHHMM')
})
} else {
await punishmentSchema.create({
_id: guild.id,
userId: member.id,
totalPunishments: 1,
warnings: 1,
mutes: 0,
kicks: 0,
lastPunishment: timestamp('YYYYMMDDHHMM')
})
}
} catch(e) {
console.log(e);
} finally {
mongoose.connection.close();
}
这是我的模式:
const mongoose = require('mongoose');
const reqString = {
type: String,
required: true
}
const punishmentSchema = mongoose.Schema({
_id: reqString,
userId: reqString,
totalPunishments: Number,
warnings: Number,
mutes: Number,
kicks: Number,
lastPunishment: reqString
})
module.exports = mongoose.model('user', punishmentSchema);
有人能帮我吗?你可能需要将你的2美元inc道具合并成一个
$inc: {'totalPunishments': 1, 'warnings': 1},
是的,伙计,谢谢!你能解释一下为什么这样做吗?因为你不能对一个对象中的不同键/值对使用同一个键,而且第二个$set可能会覆盖第一个$set,或者你会得到一个异常