findOneAndUpdate return updated field code example
Example 1: mongodb findoneandupdate return updated
Cat.findOneAndUpdate({age: 17}, {$set:{name:"Naomi"}}, {new: true}, (err, doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
console.log(doc);
});
Example 2: findoneandupdate new true
const filter = { name: 'Will Riker' };
const update = { age: 29 };
await Character.countDocuments(filter);
let res = await Character.findOneAndUpdate(filter, update, {
new: true,
upsert: true,
rawResult: true
});
res.value instanceof Character;
res.lastErrorObject.updatedExisting;