Currently, I am developing a program that tracks different "accepts" and "denies". In this project, I am utilizing the findOneAndUpdate function from mongoose. However, I am encountering some issues with achieving the desired functionality. The data I am working with includes 6 values: userID, accept, deny, nsfw, copyright, and invalid_format.
Displayed below is the code snippet:
if (interaction.customId === 'accept') {
leaderboard.findOneAndUpdate(
{
userID: interaction.user.id
},
{
userID: interaction.user.id,
$inc: { accepts: 1 }, // Increment accepts by 1
},
{
upsert: true, new: true
}, (err: any, doc: any) => {
if (err) console.log(err)
console.log(`Updated ${interaction.user.username}'s accepts to ${doc.accepts} `)
})
}
if (interaction.customId === 'deny') {
leaderboard.findOneAndUpdate(
{
userID: interaction.user.id
},
{
userID: interaction.user.id,
$inc: { denies: 1 }, //increment the denies by 1
},
{
upsert: true, new: true
}, (err: any, doc: any) => {
if (err) console.log(err)
console.log(`Updated ${interaction.user.username}'s denies to ${doc.denies} `)
})
}
if (interaction.isStringSelectMenu()) {
if (interaction.customId === 'reason') {
if (reason === 'nsfw') {
// Find the user in the database and increment the nsfw field by 1
leaderboard.findOneAndUpdate({ userID: interaction.user.id }, { userID: interaction.user.ID, $inc: { nsfw: 1 } }, { upsert: true, new: true }, (err: any, doc: any) => {
if (err) console.log(err)
console.log(`Updated ${interaction.user.username}'s ${reason}'s to ${doc.nsfw} `)
})
}
if (reason === 'copyright') {
// Find the user in the database and increment the copyright field by 1
leaderboard.findOneAndUpdate({ userID: interaction.user.id }, { userID: interaction.user.ID, $inc: { copyright: 1 } }, { upsert: true, new: true }, (err: any, doc: any) => {
if (err) console.log(err)
console.log(`Updated ${interaction.user.username}'s ${reason}'s to ${doc.copyright} `)
})
}
if (reason === 'invalid_format') {
// Find the user in the database and increment the invalid_format field by 1
leaderboard.findOneAndUpdate({ userID: interaction.user.id }, { userID: interaction.user.ID, $inc: { invalid_format: 1 } }, { upsert: true, new: true }, (err: any, doc: any) => {
if (err) console.log(err)
console.log(`Updated ${interaction.user.username}'s ${reason}'s to ${doc.invalid_format} `)
})
}
}
Despite my efforts, it seems to be generating new entries instead of updating the existing one with the same userID
. How can I resolve this issue?