javascript 如何使用mongoose的多个增量

nwlqm0z1  于 2023-01-07  发布在  Java
关注(0)|答案(1)|浏览(154)

我正在做一个程序,它可以计算不同类型的"接受"和"拒绝"。我正在使用mondgoose的findOneAndUpdate。我不能让它做我想做的事情。我有6个值,userID,accept,deny,nsfw,copyright,和invalid_format。
下面是我的代码:

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} `)
                    })
                }
            }

这会不断生成新条目,而不是使用相同的userID更新条目。如何解决此问题?

nlejzf6q

nlejzf6q1#

增量和上插不能一起工作。你必须确保它在增量之前存在。
类似这样的方法应该可以奏效:

//make sure the user's record exists
await leaderboard.updateOne(
  { userID: interaction.user.id }, 
  { $setOnInsert: { accepts: 0, denies: 0 } }, //defaults for insert
  { upsert: true }
) 

if (interaction.customId === 'accept') {          
  leaderboard.findOneAndUpdate(
    {
      userID: interaction.user.id
    },
    {
      $inc: { accepts: 1 }, // Increment accepts by 1
    }, 
    (err: any, doc: any) => {
      if (err) console.log(err)
      console.log(`Updated ${interaction.user.username}'s accepts to ${doc.accepts} `)
    }
  )
} else if (interaction.customId === 'deny') {
  leaderboard.findOneAndUpdate(
    {
        userID: interaction.user.id
    },
    {
        $inc: { denies: 1 }, //increment the denies by 1
    },
    (err: any, doc: any) => {
      if (err) console.log(err)
      console.log(`Updated ${interaction.user.username}'s denies to ${doc.denies} `)
    }
  )
}

相关问题