javascript - Nodejs, MongoDB - Determine if Document in Collection was Updated or Inserted -


i calling mongodb update function {upsert:true} insert new document if given _id not exist. determine if document inserted or update. question found using java using nodejs.

how check if document updated or inserted in mongodb

here db call.

app.post('/mongosubmit', function(req, res) {  console.log("this req.body" + json.stringify(req.body, null, 4));  var updatecustomer = function(db, callback){      db.collection('customers1').update(         {_id:req.body.email},              { first: req.body.firstname,               last: req.body.lastname,               phone: req.body.phone,               email: req.body.email,               subjectindex: req.body.subject,               messageindex: req.body.message             },          { upsert: true},          function(err, result){             if(err){console.log("database error" + err)}             callback(result);         }     ); }  mongoclient.connect(url, function(err, db){     updatecustomer(db, function(result){      console.log("these results" + json.stringify(result, null, 4));  /* ** return either * these results{     "ok": 1,     "nmodified": 0,     "n": 1,     "upserted": [         {             "index": 0,             "_id": "sjr6asdfsadfsadf28@gmail.com"         }     ] }  /* * * or      these results{     "ok": 1,     "nmodified": 1,     "n": 1 }  //but  *************** problem using value *********************     console.log("this value of modified" + result.nmodified);  /* ** returns undefined */               if(result.nmodified == 1){                 console.log("updated document");             }             else{                 console.log("inserted document");             }          db.close();          res.render('applications', {             title:"title"         });     }); });  }); 

i have tried test doing

    if(result.hasownproperty('upserted'){         //log insert     if(result.upserted == true {         //log insert     if(result.nmodified == 1){         // log update     if(result.nmodified == true){         //log update 

and adding upserted parameter callback found different forum.

function(err, result, upserted){    //callback function    //upserted undefined }) 

my result confusing. how log object property value when try log specific property comes undefined?

could explain why might happen in javascript?

or

suggest solution determining if document in collection updated or inserted?



thank you

the result structure contatins it's own property "result" has sub-properties. need inspect @ right level:

var async = require('async'),     mongodb = require('mongodb'),     mongoclient = mongodb.mongoclient;   mongoclient.connect('mongodb://localhost/test',function(err,db) {    db.collection('uptest').update(     { "a": 1 },     { "$set": { "b": 2 } },     { "upsert": true },     function(err,result) {       if (err) throw err;        if (result.result.hasownproperty('upserted') ) {         console.log( json.stringify( result.result.upserted,  undefined, 2 ) );       }        console.log( "matched: %d, modified: %d",           result.result.n,           result.result.nmodified       );     }   );  }); 

on first run "array" of "upserted" this:

[   {     "index": 0,     "_id": "55a4c3cfbe78f212535e2f6a"   } ] matched: 1, modified: 0 

on second run same values nothing added or modified:

matched: 1, modified: 0 

change value of "b" , "modified" counted since data changed.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -