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 - 使用 Git Clone 安装 NPM

javascript - 为什么 Javascript 映射函数 String.toLowerCase 在 nodejs 6 中不起作用?

javascript - 如何为 Promise 解析添加延迟?为 Node http.request Promise 添加延迟

node.js - 是否可以使用 JSX 进行 React 工作,无需 Node 服务器并使用 create-react-app

node.js - MongoDB count() 未定义

javascript - 查看页面时根据用户权限删除或隐藏 html 链接

javascript - 使用 AJAX 请求获取数组并将其传递到 php 数组

javascript - 如何在koa中使用 "http"模块?

node.js - 将 mongodb 数据解析为 javascript 数组对象

java - 如何将这样的sql查询转换为mongodb查询