简体   繁体   English

如何从 Mongoose/MongoDB 中的数组中删除元素?

[英]How to remove a element from an Array in Mongoose/MongoDB?

I have a small, yet important issue with my code that uses Mongoose/MongoDB.我的代码使用 Mongoose/MongoDB 有一个小而重要的问题。 I can't seem to be able to use $pull or $pullAll in Model.findOneAndUpdate({}) .我似乎无法在Model.findOneAndUpdate({})中使用$pull$pullAll

My code is the following:我的代码如下:

db?.updateOne({ $pull: {
                AutoRole: {
                    Roles: [value],
                }
            }
            });

And this is my Model schema:这是我的 Model 架构:

const guildSchema = new mongoose.Schema({

    GuildID: String,
    AuditChannel: String,
    AutoRole: {
        Roles: Array
    },

});

So far I'm out of ideas on how to make it work.到目前为止,我对如何使它工作没有想法。 I was wondering if I was doing it wrong, but I can't seem to find what I'm doing exactly wrong.我想知道我是否做错了,但我似乎无法找到我做错了什么。

I think the correct syntax here is:我认为这里的正确语法是:

{
  $pull: {
    "AutoRole.Roles": value
  }
}

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

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