how to use findoneand update in mongodb having id code example
Example 1: mongoose findoneandupdate
const doc = await CharacterModel.findOneAndUpdate(
{ name: 'Jon Snow' },
{ title: 'King in the North' },
{ new: true }
);
doc.title;
Example 2: 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);
});