Node.js – Mongoose findOneAndUpdate not working

mongoosenode.js

I am trying to use 'findOneAndUpdate' in mongoose and the updated JS object I am sending is not getting saved to mongo. I do not get an error upon saving, but I do get back a null for the updated object. Any ideas what I might be doing wrong? This is example is trying to update the entire object as stored in mongo, i.e. overwrite the name object.

var query = {"_id": id};
var update = {name: {first: 'john', last: 'smith'}};
var options = {new: true};
People.findOneAndUpdate(query, update, options, function(err, person) {
  if (err) {
    console.log('got an error');
  }

  // at this point person is null.
});

Best Answer

Turns out that the id I was searching for did not exist, hence the null return. Works as expected!

Related Topic