【问题标题】:MongoDB findOneAndUpdate not updatingMongoDB findOneAndUpdate 不更新
【发布时间】:2021-11-20 21:04:39
【问题描述】:

所以,我已经使用 MongoDB 工作了几天,我一直在执行每个服务器的静音角色命令,但是当我执行 set muterole 命令时,使用 findOneAndUpdate 函数,它似乎没有更新值,我一直在寻找其他答案,我已经尝试了多个,但它似乎不起作用,这是我的代码:

muteSchema.findOne(
                {
                    guildId: message.guild.id
                }, async (err, data) => {
                    if(err) console.log(err)
                    if(data)
                    {
                        muteSchema.findOneAndUpdate(
                            {
                                guildId: data.guildId
                            },
                            {
                                guildId: message.guild.id,
                                roleId: role.id
                            }
                        )
                    }
                    else
                    {
                        data = new muteSchema(
                            {
                                guildId: message.guild.id,
                                roleId: role.id
                            }
                        )
   
                        data.save().catch((err) => console.log(err) )
                    }
                }
            )

我确实在顶部有 muteSchema 导入,这是我的 muteSchema 文件:

const mongoose = require("mongoose")

let MuteroleSchema = mongoose.Schema(
    {
        guildId: String,
        roleId: String
    },
    {
        strict: false
    }
)

module.exports = mongoose.model("muterole", MuteroleSchema)

这些值在第一次使用时被注册,但一旦出现就不会更新。

【问题讨论】:

    标签: javascript mongodb mongoose discord.js


    【解决方案1】:

    你把事情弄得太复杂了。为什么先找再找再更新? 只需按照以下流程进行

    Update using filter
    

    代码示例:

    exports.updateToken = async (id, forgotToken) => {//function
    return User.updateOne({ _id: id }, { resetPasswordToken: forgotToken });//_id:id is a filter while the reset is code to update 
    }
    

    【讨论】:

      猜你喜欢
      • 2022-08-02
      • 2019-05-21
      • 2019-03-14
      • 1970-01-01
      • 2019-02-20
      • 2019-04-19
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      相关资源
      最近更新 更多