2017-02-14 45 views
2

我试图更新JSON场“champ_x”从1到3并在同一时间在一个动态函数双方球员1:更新里面的对象与猫鼬信息,内部对象数组动态

{ 
    "_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 
} 

我有这个模型:

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

这个优良样板工程

我的问题是,我试图让它动态的,我可以通过函数只发送参数的当前转(1或2)和t他选择了位置(冠军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; 

但我得到的是说“意外令牌+”好像串联值不起作用的错误。有没有办法做到这一点?

谢谢!

+0

[Nodejs Mongo insert into subdocument - dynamic fieldname](http://stackoverflow.com/questions/12184626/nodejs-mongo-insert-into-subdocument-dynamic-fieldname) – dNitro

回答

1

您可以建立$set修改和匹配部分由@dNitro的建议:

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