使用Mongoose更新信息,动态地在对象内的对象内部

时间:2017-02-14 19:35:53

标签: javascript node.js mongodb mongoose

我试图更新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)和所选位置(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;

但我收到的错误是"意外的令牌+"似乎连接价值并不起作用。有没有办法做到这一点?

谢谢!

1 个答案:

答案 0 :(得分:1)

您可以根据@dNitro的建议构建$set修饰符和匹配部分:

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