update statement in mongoose code example
Example 1: mongoose updateone example
// Update the document using `updateOne()`
await CharacterModel.updateOne({ name: 'Jon Snow' }, {
title: 'King in the North'
});
// Load the document to see the updated value
const doc = await CharacterModel.findOne();
doc.title; // "King in the North"
Example 2: save or update mongoose
// SAVE NEW OR UPDATE EXISTING COLLECTION
AnnoucementTagsModel.findOneAndUpdate({_id: announcementId}, newAnnoucementTags, {upsert: true}, function (err, doc) {
if (error) throw new Error(error);
console.log("succesfully saved");
});
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)
})