How to save / update multiple documents in mongoose How to save / update multiple documents in mongoose mongoose mongoose

How to save / update multiple documents in mongoose


You can use update query with multi:true which update all documents in your db.please find below reference code,

 model.update({ "_id": id }, { $set: { "Key": "Value" } }, { multi: true }, function (err, records) {      if (err || !records) {        return res.json({ status: 500, message: "Unable to update documents." });      } else {        return res.json({ status: 200, message: "success" });      }    });


If you are trying to make the same change to each document in the results, you could do something like this:

model.update({ _id: { $in: results.map(doc=>doc._id) }}, { yourField: 'new value' }, { multi: true })