繁体   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;

  })

我在mongodb中使用流星。 我正在尝试使用var将数据库收入$ inc

我有一行是:

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

没有任何反应,我已定义了所有变量,但似乎无法通过变量增加变量? 也许语法错误?

另一方面,这可以工作:

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

在任何更新操作中使用另一个字段

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

或者,如果您不需要强大的功能,请在1个以下使用

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

不要引用'power' 但是,您还必须首先获取权力的价值。

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