繁体   English   中英

如何通过mongoose,Node.js中的var设置密钥?

[英]how to set key by var in mongoose,Node.js?

我没有设置变量更新的密钥,我的代码......

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

如果我使用

var this_key = 'test';

但是this_key不是'test',而是'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){});

暂无
暂无

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

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