動的

2017-02-14 36 views
2

私は1から3までと動的機能で一度に両方のプレイヤー1用のJSONフィールド「champ_x」を更新しようとしている:動的

{ 
    "_id": { 
     "$oid": "58a3521edf127d0a0c417cda" 
    }, 
    "room": "room_0.0940045412694186", 
    "player_1": "eee", 
    "player_2": "fff", 
    "player_1_details": { 
     "history_moves": [], 
     "champions": [ 
      { 
       "champ_1": "na" 
      }, 
      { 
       "champ_2": "na" 
      }, 
      { 
       "champ_3": "na" 
      } 
     ] 
    }, 
    "player_2_details": { 
     "history_moves": [], 
     "champions": [ 
      { 
       "champ_1": "na" 
      }, 
      { 
       "champ_2": "na" 
      }, 
      { 
       "champ_3": "na" 
      } 
     ] 
    }, 
    "game_state": "789", 
    "__v": 0 
} 

1(

match_schema.statics.update_champ = function(room, turn, champ_num, champ_select, callback){ 
    if(champ_num == "champ_1"){ 
     match_mongoose.update({ room: room }, { $set: { 'player_1_details.champions.0.champ_1': champ_select}}) 
     .exec(function(error){ 
      if(error){ return callback(error); }else{ return callback(null); } 
     }); 
    } 
}; 

このモデルは

罰金私の問題は、私はそれを動的にしようとしている、ある作品、私はちょうど関数を使用して送信することができている現在のターンパラメータ:私はこのモデルが持っていますまたは2)、およびt彼はポジションを選んだ(champ_1,2、または3)。

//Update Champion 
match_schema.statics.update_champ = function(room, turn, champ_num, champ_select, callback){ 
    match_mongoose.update({ room: room }, { $set: { 'player_'+turn+'_details.champions.0.'+champ_num: champ_select}}) 
    .exec(function(error){ 
     if(error){ return callback(error); }else{ return callback(null); } 
    }); 
}; 

var match_mongoose = mongoose.model('matches', match_schema, 'matches'); 
module.exports = match_mongoose; 

しかし、私は、「予期しないトークン+」が動作しない値を連結するように思えるというエラーが出ます:

私はこれを試してみました。これを行う方法はありますか?

ありがとうございます! @dNitroにより示唆されるようにあなたが$set修飾子と一致する部分を構築することができる

+0

[Nodejs Mongo insertサブ文書に - 動的フィールド名](http://stackoverflow.com/questions/12184626/nodejs-mongo-insert-into-subdocument-dynamic-fieldname) – dNitro

答えて

1

var modifier = { $set: {} }; 
modifier.$set['player_' + turn + '_details.champions.$.champ_' + champ_num] = champ_select; 

をあなたはまた、配列のインデックスに問題があるでしょう、あなたはchampions.0を指定するので、それは常に、最初の配列項目がかかりますchamps_2 & champs_3と一致しません。このため一つの解決策は、アレイからの一致で位置パラメータ$を使用することです:

var match = {}; 
match['room'] = room; 
match['player_' + turn + '_details.champions.champ_' + champ_num] = { $exists: true }; 

フル更新機能は次のとおりです。

matchSchema.statics.update_champ = function(room, turn, champ_num, champ_select, callback) { 

    var modifier = { $set: {} }; 
    modifier.$set['player_' + turn + '_details.champions.$.champ_' + champ_num] = champ_select; 

    var match = {}; 
    match['room'] = room; 
    match['player_' + turn + '_details.champions.champ_' + champ_num] = { $exists: true }; 

    this.update(match, modifier) 
     .exec(function(error) { 
      if (error) { 
       return callback(error); 
      } else { 
       return callback(null); 
      } 
     }); 
}; 

し、それを呼び出す:

Match.update_champ("room_0.0940045412694186", 1, 1, "new_value", function(err, res) { 
    if (!err) { 
     console.log(err); 
     return; 
    } 
    console.log(res); 
}); 

あなたは完全な例を見つけることができますhere