繁体   English   中英

使用 mongoose 更新 object

[英]Updating an object using mongoose

我正在尝试使用此代码更新我的数据库中的 object。

if (args[1] === 'set'){
            serverModel.findOneAndUpdate(
                {serverID: message.guild.id},
                {$set: {'restraints.channel': message.channel.id}}
            )
        }

问题是,虽然代码运行没有错误,但数据库中的任何内容都没有更新。

它遵循这个 model,但我似乎无法更新“频道”:

const serverSchema = new mongoose.Schema({
    serverID: {type: String, require: true, unique: true},
    restraints: {type: Object, require: true, default: {channel: null, locked: false, announce: null}}
});

serverModel.findOneAndUpdate()除了返回Query之外什么都不做。

在数据库发生任何事情之前,需要执行查询。

有多种方法可以做到这一点:

// preferred way:
await serverModel.findOneAndUpdate(…)

// OR, pass a callback:
serverModel.findOneAndUpdate(…, (err, doc) => { … })

// OR, just execute it and ignore the outcome:
serverModel.findOneAndUpdate(…).exec();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM