The findOneAndUpdate() method Updates the values in the existing document Update a single document which matches the given filter updates all the documents that matches the given filter replaces the existing document with the new document code example

Example: mongofindoneandupate

try {
db.grades.findOneAndUpdate(
   { "name" : "A.B. Abracus" },
   { $set: { "name" : "A.B. Abracus", "assignment" : 5}, $inc : { "points" : 5 } },
   { sort: { "points" : 1 }, upsert:true, returnNewDocument : true }
);
}
catch (e){
   print(e);
}

Tags:

Misc Example