Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 327
Phatsin.lk Avatar asked Jun 25 '12 04:06

Phatsin.lk


1 Answers

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){});
like image 167
Thilo Avatar answered Sep 23 '22 04:09

Thilo