node.js - Mongo values not updating -
i'm trying increment value of object in mongoose , seems not want go past 2?? there i'm missing? i'm new mongo
campsite.findone({slug : req.body.campsite.slug }, function(err, site){ if(!site.tracking){ console.log("no previous tracking @ all"); site.tracking = {}; } if(site.tracking[req.body.tracking_type] == undefined){ console.log("no previous tracking of type"); site.tracking[req.body.tracking_type] = 1; }else{ console.log("well count then!"); site.tracking[req.body.tracking_type] += + 1; } site.save(function (err, fluffy) { console.log(err); }); res.send(200); });
it wont save updated result? schema below
var campsitesschema = new schema({ name: string, address_line_1: string, address_line_2: string, county: string, postcode: string, loc: array, website: string, email: string, telephone_1: string, telephone_2: string, description: string, slug:string, status:string, confirm_url_key: string, password: string, short_description: string, long_description: string, features: array, tracking: {} });
you're better off using update $inc modifier
campsite.update({slug : req.body.campsite.slug }, {$inc: { 'tracking[' + req.body.tracking_type + ']' : 1} });
Comments
Post a Comment