1// This will create another document if it doesn't exist
2findByIdAndUpdate(_id, { something: 'updated' }, { upsert: true });
1var conditions = { name: 'bourne' }
2 , update = { $inc: { visits: 1 }}
3
4Model.update(conditions, update, { multi: true }).then(updatedRows=>{
5
6}).catch(err=>{
7 console.log(err)
8
9})
1const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
2res.n; // Number of documents matched
3res.nModified; // Number of documents modified
1// SAVE NEW OR UPDATE EXISTING COLLECTION
2AnnoucementTagsModel.findOneAndUpdate({_id: announcementId}, newAnnoucementTags, {upsert: true}, function (err, doc) {
3 if (error) throw new Error(error);
4 console.log("succesfully saved");
5});