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

223
Views
Deleting an item from a MongoDB document

I'm using Node.js. I got this document:

   {
   users: [ '614e000c826b2308ec49508d', '614e0021826b2308ec49508e' ],
   _id: 61d5fe6af420232658ffe131,
   name: 'Standard',
   __v: 0
   }

Which I assigned to a variale groupToEdit

I'm trying to remove one of the elements in the users array with this line:

    await groupToEdit.updateOne(
      { _id: '61d5fe6af420232658ffe131' },
      { $pull: { 'users': '614e000c826b2308ec49508d' } }
    )

But nothing is removed. What am I doing wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use this style:

await yourCollection.updateOne({
  _id: mongoose.Types.ObjectId('61d5fe6af420232658ffe131')}, {
  $pull: { 'users': '614e000c826b2308ec49508d' }
});

Your problem is that mongoDB store _id as ObjectId and you are passing it as a string. Type of the id you pass is important.

over 4 years ago · Santiago Trujillo 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!