Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!