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

306
Views
Node.js MongoDB - How to update multiple documents with Mongoose?

I have a users collection and every user has a number of contacts. When a user deletes their account I want that this user's id would be deleted from the contacts array of all the users with who this user is connected. I have tried this Model.Update query but it doesn't work. Here is my code so far:

                User.update({'userId':{ $in: userIds }, 
                        $pullAll: {'contacts': [myId] },'multi': true 
                    },function(err, count) {
                        if(err){
                            console.log(err);
                        }else{
                            console.log(count);
                        } 
                    });
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The update document and multi should be passed as separate arguments:

User.update({
  userId : { $in : userIds }        // conditions
}, {
  $pullAll : { contacts : [myId] }  // document
}, {
  multi : true                      // options
}, function(err, count) {
  if (err) {
    console.log(err);
  } else {
    console.log(count);
  }
});

Documentation here.

over 4 years ago · Santiago Trujillo Report

0

Can update multiple documents with multiple conditions

Model.update({
    _id : { $in : ids}        // conditions
}, {
    $set: {deletion_indicator: constants.N} // document
}, {
    multi : true                      // options
}, function(err, result) {
    if (err) {
        console.log(err);
    } else {
        console.log(result);
    }
});
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!