我没有设置变量更新的键,我的代码......
mongoose.model('members', Schema).update({ id: '0' }, {$push: {'this_key': 'value'}} , [], function (err, data){});
if I use
var this_key = 'test';
但 this_key 不是“测试”,而是“this_key”
mongoose.model('members', Schema).update({ id: '0' }, {$push: {this_key: 'value'}} , [], function (err, data){});
我需要获取一些值 ext POST[] 来设置变量 this_key,
如何在 mongoose,Node.js 中通过变量设置键?
对象字段名称中字符串文字的语法在这里让您感到困惑。要解决这个问题,请创建一个中间对象并在不使用文字的情况下构造它:
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){});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)