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
Post a Comment