Raul Almonte
Raul Almonte

Reputation: 13

Find all users and update them at once

is there a way to find all users and update them at the same time?

This code works with findOneAndUpdate or findByIdAndUpdate, but only remove the record from one user.

I need to remove the objects inside "topicsfollowed" that match the (req.params.id) from all users at the same time.

Thanks in advance.

User.find({}, { $pull: {topicsfollowed: req.params.id}}, function (err){
    if (err) {
        console.log(err);
        res.render('index.ejs', { errors:errors });
      }
      res.send('Success')
  })

Upvotes: 0

Views: 36

Answers (1)

user2667714
user2667714

Reputation: 36

Note

1) {multi:true} to update Multiple documents in mongoose .

2) use update query to update Multiple documents ,If you are using findOneAndUpdate query only one record will be updated.

 User.update({},{$pull: {topicsfollowed:req.params.id}},{multi: true}, function(err, result) {

})

Upvotes: 2

Related Questions