Alex Hong
Alex Hong

Reputation: 95

Mongdb $inc by variable

      Accounts.onCreateUser(function(options, user) {


        user.cheat = "(Member)"
        user.cost = 1000;
        user.money = 0;
        user.rate = 0;
        user.spy = 200;
        user.adv = 10; 
        user.power = 25;
        return user;

  })

I'm using meteor with mongodb. I'm trying to $inc a database thingy with a var

I have a line that is:

click: function () {    
Meteor.users.update({_id: this.userId}, {$inc: {'money': 'power'  }});
},

Nothing happens I have all the vars defined but I can't seem to be able to increment a var by a var? Maybe wrong syntax?

This on the other hand works:

click: function () {    
Meteor.users.update({_id: this.userId}, {$inc: {'money': 25  }});
},

Upvotes: 0

Views: 131

Answers (2)

skmahawar
skmahawar

Reputation: 311

User another field in any update operator.

    var variable2 = 1;
   click: function () {    
    Meteor.users.update({_id: this.userId}, 
      {$inc: {'money': this.power },$inc: {'power': variable2 } });
    },

Or if you don't want increament power then use below one

   var variable2 = 1;
   click: function () {    
 Meteor.users.update({_id: this.userId}, 
            {$inc: {'money': this.power } });
    },

Upvotes: 2

Michel Floyd
Michel Floyd

Reputation: 20246

Don't quote 'power'! But also you have to fetch the value of power first.

click: function () {    
  var power = Meteor.user().power;
  Meteor.users.update({_id: this.userId}, {$inc: {'money': power  }});
},

Upvotes: 1

Related Questions