Reputation: 1346
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?
Upvotes: 6
Views: 4678
Reputation: 262464
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){});
Upvotes: 13