MongoDB, removing a specific object with one value name from an array

Viewed 20

Please pardon me for my bad explanation, I'm very new to coding. (discord bot)

So I have an array with 2 required values (character name & shards) and 1 optional value (character image link) It's basically a function where each user can have multiple 'characters' and I want to remove one character using just the name of the character as an input, from that user's data when a command is run.

This is my data right now:

{
    "_id" : ObjectId("62593f2380fea1cee9f986ed"),
    "userID" : "321311458459779073",
    "characters" : [ 
        {
            "characterName" : "Akasuka",
            "characterImg" : "",
            "shards" : "0"
        }, 
        {
            "characterName" : "Nigeria",
            "characterImg" : "",
            "shards" : "0"
        }
    ],
    "__v" : 3
}

My code to remove it:

(prof = database that I required at the top using const prof = require("../../Schemas/ProfileDB");) (cName = input from the command)

prof.findOne({ userID: pUser.id }, async (err, data) => {
          if (err) throw err;
          if (data) {
            const id = data._id

            prof.updateOne(
              { userID: pUser.id },
              { $pull: {'characters': {characterName: cName}}},
              { safe: true }
            )

            data.save()
          }
        })

And, well, as you might have guessed it does not remove the object from the array.

0 Answers
Related