6

私は変数を更新のキーに設定していません、私のコード...

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){});

変数 this_key を設定するには、値 ext POST[] を取得する必要があります。

mongoose、Node.jsで変数ごとにキーを設定する方法は?

4

1 に答える 1

13

オブジェクトフィールド名の文字列リテラルの構文は、ここであなたを悩ませています。それを回避するには、中間オブジェクトを作成し、リテラルを使用せずに構築します。

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){});
于 2012-06-25T04:24:46.077 に答える