0

The problem here is that I'm only updating the first document with the given email but I want to update all the emails he found with this email so I want to use find() but I don't know what to write to update all documents that was found


router.route("/Updateinfo").post((req, res) => {
  console.log("updateinfo");
 console.log(email);
 const z = req.body.Name;
 const d = req.body.Password;
 const s = req.body.Email;
 const v = req.body.Type;
 console.log(z);
if(v!="admin"){
 User.findOne({Email: email}, function (err, user) {
  user.Name = z;
  user.Password = d;
  user.Email = s;
  user.Type = v;

 
  user.save(function (err) {
      if(err) {
          console.error('ERROR!');
      }
  });
});
  • 1
    Does this answer your question? [How can I update multiple documents in mongoose?](https://stackoverflow.com/questions/6694507/how-can-i-update-multiple-documents-in-mongoose) – Enver Arslan Nov 30 '21 at 20:43

1 Answers1

1

You need to use updateMany if you want to update multiple documents that match the same query with the same data.

See https://mongoosejs.com/docs/api.html#model_Model.updateMany

Ayzrian
  • 2,279
  • 1
  • 7
  • 14