Update model with Mongoose, Express, NodeJS

Now, i think you can do this :

Place.findOneAndUpdate({name:req.params.name}, req.body, function (err, place) {
  res.send(place);
});

You can find by id too :

Place.findOneAndUpdate({_id:req.params.id}, req.body, function (err, place) {
  res.send(place);
});

You have to find the document before updating anything:

Place.findById(req.params.id, function(err, p) {
  if (!p)
    return next(new Error('Could not load Document'));
  else {
    // do your updates here
    p.modified = new Date();

    p.save(function(err) {
      if (err)
        console.log('error')
      else
        console.log('success')
    });
  }
});

works for me in production code using the same setup you have. Instead of findById you can use any other find method provided by mongoose. Just make sure you fetch the document before updating it.


So now you can find and update directly by id, this is for Mongoose v4

Place.findByIdAndUpdate(req.params.id, req.body, function (err, place) {
  res.send(place);
});

Just to mention, if you needs updated object then you need to pass {new: true} like

Place.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, place) {
  res.send(place);
});