node.js - mongoose update : $inc is not working in upsert -


i trying update document if exist or insert if not exist.i have 1 field $inc,( want increment previous value one). code

var appuser = new appusers({    imei: req.body.imei,    $inc : {user_count:1},    install_flag : 1 }); var upsertdata = appuser.toobject();  delete upsertdata._id;  appusers.update({imei: appuser.imei}, upsertdata, {upsert: true}, function(err, data){     if(err) return console.log(err);    res.send(data); }); 

only $inc not working. in schema have user_count : { type:number, default:0}

i'd suggest this:

appusers.update({imei: appuser.imei}, {$set: {imei: req.body.imei, install_flag : 1}, $inc : {user_count:1}}, {upsert: true}, function(err, data){     if(err) return console.log(err);    res.send(data); }); 

or if want use object:

var upsertdata = {       $set: {imei: req.body.imei, install_flag : 1},        $inc: {user_count: 1} };  appusers.update({imei: appuser.imei}, upsertdata, {upsert: true}, function(err, data){     if(err) return console.log(err);    res.send(data); }); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -