mongoose findOneAndUpdate return updated doc code example
Example 1: returned value by findOneAndUpdate
const filter = { age: 17 };
const doc = { $set: { name: "Naomi" } };
const options = { new: true };
Cat.findOneAndUpdate(filter, doc, options, (err, doc) => {
if (err) console.log("Something wrong when updating data!");
console.log(doc);
});
Example 2: findone and update mongoose
Model.findOne({ name: 'Mr. Anderson' }).
then(doc => Model.updateOne({ _id: doc._id }, { name: 'Neo' })).
then(() => Model.findOne({ name: 'Neo' })).
then(doc => console.log(doc.name));
const doc = await Model.findOne({ name: 'Neo' });
console.log(doc.name);