I don't set variable the key for update, my code...
mongoose.model('members', Schema).update({ id: '0' }, {$push: {'this_key': 'value'}} , [], function (err, data){});
if I use
var this_key = 'test';
but this_key is not 'test' it's 'this_key' in
mongoose.model('members', Schema).update({ id: '0' }, {$push: {this_key: 'value'}} , [], function (err, data){});
I need get some value ext POST[] to set variable this_key,
how to set key by variable in mongoose,Node.js?
The syntax for string literals in object field names is biting you here. To get around it, make an intermediate object and construct it without using literals:
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){});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With