find and update mongoose 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: 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);
Example 3: update query in mongoose
var conditions = { name: 'bourne' }
, update = { $inc: { visits: 1 }}
Model.update(conditions, update, { multi: true }).then(updatedRows=>{
}).catch(err=>{
console.log(err)
})