javascript - 使用 Mongoose 更新信息,对象内部的数组动态地更新

标签 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;

但我收到一条错误消息,指出“意外标记 +”似乎无法连接值。有办法做到这一点吗?

谢谢!

最佳答案

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

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

您还会遇到数组索引的问题,您指定 champions.0 因此它将始终采用与 champs_2 不匹配的第一个数组项 & 冠军_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

关于javascript - 使用 Mongoose 更新信息,对象内部的数组动态地更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42234713/

相关文章:

javascript - 使用 nuxt.js 导入文件(Vue 相当于 getElementById())

javascript - 按钮的 OnClick 事件警报

sql - 使用 Sequelize 作为 ORM 在 Sails 应用程序中使用原始查询

mongodb - 从 mongo 返回数据表的正确方法

javascript - 如何获得上午或下午?

javascript - Greasemonkey 无法找到/修改/删除内容? (在 Twitch.tv 上)

node.js - 在 mongoDB 中使用 Monk 限制查找

javascript - 是否可以从另一个包导入 Node 包?

java - MongoDB/Morphia 中的引用

mongodb - 在使用 CLI 创建的 MongoDB 分片集合中插入文档时出错