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

259
Views
Remove Object from array in Mongoose

I'm having this problem when removing an Object inside Array. I don't seems to understand what is the proper way of removing an item using $pull. Thanks in advance!

JSON data

"accountId": "62efd8c008f424af397b415d",
"links": [
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub",
        "_id": "62efe1ee4675d0baa10bd219"
    },
    {
        "url": "www.github.com/iluvkohii",
        "title": "GitHub",
        "linkId": "62efe4e142099faeeb5999d0"
    }
]

Code

  try {
    const { accountId, _id } = req.body;
    return Profile.findOneAndUpdate(
      { accountId },
      { $pull: { links: { _id } } },
      { new: true, }
    )
      .then((value) => res.status(200).json(value))
      .catch((err) => res.status(400).json(err.errors));
  } catch (error) {
    console.error(error);
  }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I just solved my own question after a day realizing that a normal String is not equal to Mongoose ObjectId

  try {
    const { accountId, _id } = req.body;
    return Profile.findOneAndUpdate(
      { accountId },
      { $pull: { links: { id: mongoose.Types.ObjectId(_id) } } },
      { new: true },
    )
      .then((value) => res.status(200).json(value))
      .catch((err) => res.status(400).json(err.errors));
  } catch (error) {
    console.error(error);
  }
about 4 years ago · Juan Pablo Isaza Report
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!