Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

243
Visualizações
MongoDB, removing a specific object with one value name from an array

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.

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda