node.js - Mongoose: how to check if document is modified via model.findOneAndUpdate() -


in mongoose, can check if update operation has modified document model.update():

model.update(query, update, function(err, raw){     if (raw.nmodified >= 1) console.log('document modified!') }); 

is there way same model.findoneandupdate()?

model.findoneandupdate(query, update, { new: true }, function(err, doc){     if (doc) {         // mongodb found document, there way          // know document indeed modified?     } }); 

you can pass option { passrawresult : true } mongoose advice mongoose pass raw result of underlying mongodb driver, in case mongodb-native, third argument callback.

mongodb-native documentation findoneandupdate

model.findoneandupdate(query, update, { new: true, passrawresult : true }, function(err, doc, res){     // res     // { value: { _id: 56a9fc80a7f9a4d41c344852, name: 'hugo updated', __v: 0 },     //   lasterrorobject: { updatedexisting: true, n: 1 },     //   ok: 1 } }); 

in case update did not succeed due no matching document found null res passed callback. in case document matched field values same before update res object not give enough information figure out if values updated matching document.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -