Mongo值不更新

时间:2014-06-27 11:24:38

标签: node.js mongodb mongoose

我试图简单地在猫鼬中增加一个对象的值,它似乎不想超过2?有什么我想念的吗?我是Mongo的新手

campsite.findOne({slug : req.body.campsite.slug }, function(err, site){

    if(!site.tracking){
        console.log("no previous tracking at all");
        site.tracking = {};
    }

    if(site.tracking[req.body.tracking_type] == undefined){
        console.log("no previous tracking of this type");
        site.tracking[req.body.tracking_type] = 1;
    }else{
        console.log("well count it then!");
        site.tracking[req.body.tracking_type] += + 1;
    }

    site.save(function (err, fluffy) {
        console.log(err);
    });

    res.send(200);
});

它不会保存更新的结果?我的架构在下面

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: {}
});

1 个答案:

答案 0 :(得分:0)

使用带有$ inc修饰符的更新

会好得多
campsite.update({slug : req.body.campsite.slug }, {$inc: { 'tracking[' + req.body.tracking_type + ']' : 1} });