如何通过mongoose中的var来设置关键字,Node.js?

我不设置variables的关键更新,我的代码…

mongoose.model('members', Schema).update({ id: '0' }, {$push: {'this_key': 'value'}} , [], function (err, data){}); 

如果我使用

 var this_key = 'test'; 

但是this_key不是'testing'它是'this_key'中的

  mongoose.model('members', Schema).update({ id: '0' }, {$push: {this_key: 'value'}} , [], function (err, data){}); 

我需要得到一些值分机POST []设置variablesthis_key,

如何通过mongoose中的variables来设置关键字,Node.js?

在对象字段名称中的string文字的语法是在这里咬你。 为了解决这个问题,创build一个中间对象并构造它而不使用文字:

 var this_key = 'test'; var push = {}; push[this_key] = 'value'; // here, it will use the variable mongoose.model('members', Schema).update( { id: '0' }, {$push: push} , [], function (err, data){});