Reputation: 13
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
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