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
修飾子と一致する部分を構築することができる
[Nodejs Mongo insertサブ文書に - 動的フィールド名](http://stackoverflow.com/questions/12184626/nodejs-mongo-insert-into-subdocument-dynamic-fieldname) – dNitro