首页 > 解决方案 > 试图检测用户何时达到 100 条消息,然后重置,并将一个添加到 caseCount,但它不起作用,我没有收到任何错误

问题描述

这是在 Discord.js 中使用 MongoDB,所有这些代码都在消息事件中

所以在 MongoDB 中,我正在尝试查看某人是否有 100 条或更多消息,如果有,它会重置他们的 messageCount,并在 messageCount 的“caseCount”中添加一个,这就是我到目前为止所拥有的,但它没有t 工作,也没有给我一个错误:

if(profileData.messageCount >= 100) {
        await Profiles.findOneAndUpdate(
          {
            userID: message.author.id,
          },
          {
            $inc: {
              messageCount: 0,
              caseCount: + 1
            }
          }
        )
      }

标签: javascriptmongodb

解决方案


$inc在 MongoDB 中,为文档的属性增加或添加某个值。您目前正在做的messageCount是向其添加 0,使其保持相同的数量。我相信您正在寻找的是将$set属性设置为所需值的关键字。

if (profileData.messageCount >= 100) {
    await Profiles.findOneAndUpdate({
        userID: message.author.id,
    }, {
        $set: {
            messageCount: 0
        }
        $inc: {
            caseCount: 1
        }
    })
}

推荐阅读