I'm making an beg command for my discord bot, but $inc isn't incrementing the value! I also have this issue for my warn command. I have to mention though that I'm not very experienced with mongodb
My code is:
const schema = require('../schemas/moneyschema')
module.exports.run = async(client, msg, args) => {
const givers = [
"Elon Musk",
"Jeff Bezos",
"Your Crush",
"Mike Oxlong"
]
const giver = givers[Math.floor(Math.random() * givers.length)]
const givenMoney = Math.floor(Math.random() * 501)
if (schema.findOne({ _id: msg.author.id })) {
schema.updateOne({ $inc: { "money": givenMoney } })
msg.reply(`${giver} gave you $${givenMoney}`)
} else {
let newData = new schema({
_id: msg.author.id,
money: givenMoney
})
newData.save()
msg.reply(`${giver} gave you your first $${givenMoney}`)
}
}
module.exports.help = {
name: "beg",
aliases: []
}
Is there anyone that is experienced with mongodb and knows how this works?
Mongoose will not predict for which guild it should increment the field for.
You haven't specified a filter which is the first argument in the updateOne method in mongoose. You can read about this here
schema.updateOne({_id: msg.author.id},{ $inc: { "money": givenMoney } })
should fix it