简体   繁体   English

Mongdb $ inc通过变量

[英]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. 我在mongodb中使用流星。 I'm trying to $inc a database thingy with a var 我正在尝试使用var将数据库收入$ inc

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  }});
},

User another field in any update operato r. 在任何更新操作中使用另一个字段

    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 或者,如果您不需要强大的功能,请在1个以下使用

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

Don't quote 'power' ! 不要引用'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  }});
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM