【问题标题】:Using the $inc function across the MongoDB documents在 MongoDB 文档中使用 $inc 函数
【发布时间】:2021-08-19 05:42:00
【问题描述】:

我目前正在为我的 Discord 机器人开发审核系统,但遇到了一个意外问题。我一直在使用 $inc 函数来增加单个文档的值,但遗憾的是我没有实现在多个不同文档中使用 $inc 函数,这意味着我想增加 ($inc) 新文档的值根据上一个文档的编号进行文档。

示例:Cases

当前代码:

async run(client, message, args, Discord) {

    const targetMention = message.mentions.users.first()
    const userid = args[0]
    const targetId = client.users.cache.find(user => user.id === userid)
    const username = targetMention.tag

    if(targetMention){
       
        args.shift()

        const userId = targetMention.id
        const WarnedBy = message.author.tag
        const reason = args.join(' ')

        if(!reason) {
            message.delete()
            message.reply('You must state the reason behind the warning you are attempting to apply.').then(message => {
                message.delete({ timeout: 6000})
            });
            return;
        }

        const warningApplied = new Discord.MessageEmbed()
        .setColor('#ffd200')
        .setDescription(`A warning has been applied to ${targetMention.tag} :shield:`)

        let reply = await message.reply(warningApplied)
        let replyID = reply.id

        message.reply(replyID)

        const warning = {
            UserId: userId,
            WarnedBy: WarnedBy,
            Timestamp: new Date().getTime(),
            Reason: reason,

        }

        await database().then(async database => {
            try{
                await warnsSchema.findOneAndUpdate({
                    Username: username,
                    MessageID: replyID
                }, {
                    $inc: {
                        Case: 1
                    },
                    WarnedBy: WarnedBy,
                    $push: {
                        warning: warning
                    }
                }, {
                    upsert: true
                })
            } finally {
                database.connection.close()
            }
        })
    }
    if(targetId){
       
        args.shift()

        const userId = message.member.id
        const WarnedBy = message.author.tag
        const reason = args.join(' ')

        if(!reason) {
            message.delete()
            message.reply('You must state the reason behind the warning you are attempting to apply.').then(message => {
                message.delete({ timeout: 6000})
            });
            return;
        }

        const warning = {
            userId: userId,
            WarnedBy: WarnedBy,
            timestamp: new Date().getTime(),
            reason: reason
        }

        await database().then(async database => {
            try{
                await warnsSchema.findOneAndUpdate({
                    userId,
                }, {
                    $inc: {
                        Case: 1
                    },
                    WarnedBy: WarnedBy,
                    $push: {
                        warning: warning
                    }
                }, {
                    upsert: true
                })
            } finally {
                database.connection.close()
            }
            const warningApplied = new Discord.MessageEmbed()
            .setColor('#ffd200')
            .setDescription(`A warning has been applied to ${targetId.tag} :shield:`)
            message.reply(warningApplied)
            message.delete();
        })
    }
}

附加到代码的架构:

const warnsSchema = database.Schema({
    Username: {
        type: String,
        required: true
    },
    MessageID: {
        type: String,
        required: true
    },
    Case: {
        type: Number,
        required: true
    },
    warning: {
        type: [Object],
        required: true
    }
})
module.exports = database.model('punishments', warnsSchema)

【问题讨论】:

  • 您应该edit your question 并向我们展示您当前的代码。
  • 没问题!已编辑。

标签: mongodb mongoose discord.js


【解决方案1】:

回答我自己的问题。对于所有试图做与我完全相同的人来说,有一种更简单的方法可以让它正常工作。 $inc (increase) 函数不能作为文档的主要属性。在您的数据库中实现此功能的更简单方法是在 Discord 机器人文件中创建一个 .json 文件并添加如下行:

{
 "Number": 0
}

之后,您需要“npm i fs”以便实时读取目录。 您可以继续添加一个函数来增加或减少“数字”的值。 您必须确保通过键入以下内容将变量导入当前编码文档:

const {Number} = require('./config.json') 

config.json 可以任意命名,仅作为示例。

现在您可以console.log(Number) 以确保该数字是您所期望的,并且您现在可以通过输入Number+=[amount] 来增加它

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 2016-12-15
    • 2022-12-03
    • 1970-01-01
    • 2019-01-06
    • 2014-05-21
    • 1970-01-01
    • 2013-04-11
    相关资源
    最近更新 更多