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

133
Views
MongoDB Update/delete Multiple documents based on Custom ID

I have a mongo schema

   const taskSchema=new Schema({
    userID:{type:ObjectId,required:true},
  task: {
    type: String,
    required: true,
    trim: true,
    maxlength: 30,
  },
  finalDate:{type:Date,required:true},
  isFinished:{type:Boolean,required:true}
 })

what I need to get all the documents with a particular UserID and update/delete . I can use the find method to find all the documents with the required user id . After that I am at a loss on what to do.There will be multiple documents with same user id . Any ideas on how to proceed will be a great help

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Update

To update the first document matching the given condition

db.collection.update({
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
},
{
  $set: {
    "isFinished": true
  }
})

Test Here

To update all documents matching the given condition use multi: true

db.collection.update({
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
},
{
  $set: {
    "isFinished": true
  }
},
{
  multi: true
})

Test Here

Delete

To delete the first document matching the given condition

db.collection.delete({ 
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
})

To delete all documents matching the given condition

db.collection.deleteMany({ 
  "userID": ObjectId("5b87780c9ca8b03096a33380"),
  "task": "Cooking"
})
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!