Home > Software engineering >  How to remove a element from an Array in Mongoose/MongoDB?
How to remove a element from an Array in Mongoose/MongoDB?

Time:09-16

I have a small, yet important issue with my code that uses Mongoose/MongoDB. I can't seem to be able to use $pull or $pullAll in Model.findOneAndUpdate({}).

My code is the following:

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

And this is my Model schema:

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.

CodePudding user response:

I think the correct syntax here is:

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