Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更新meteor集合中的子阵列列表_Javascript_Arrays_Mongodb_Meteor - Fatal编程技术网

Javascript 更新meteor集合中的子阵列列表

Javascript 更新meteor集合中的子阵列列表,javascript,arrays,mongodb,meteor,Javascript,Arrays,Mongodb,Meteor,我在会话变量中都有游戏id和玩家id Games.insert({ board : hex_board(7), players : [{id : 0, hexIds : []}, {id : 1, hexIds : []}], }); 到目前为止,我一直坚持: Games.update(Session.get("game"), {$addToSet : {players : ""}}); 我不知道如何进一步细化它使用的播放器列表中的哪个元素,但标准在Ses

我在会话变量中都有游戏id和玩家id

  Games.insert({
      board : hex_board(7),
      players : [{id : 0, hexIds : []}, {id : 1, hexIds : []}],
  });
到目前为止,我一直坚持:

  Games.update(Session.get("game"), {$addToSet : {players : ""}});
我不知道如何进一步细化它使用的播放器列表中的哪个元素,但标准在
Session.get(“activePlayer”)

当密钥硬编码为0或1时几乎可以工作,我没有使其依赖于activePlayer var

        Games.update(Session.get("game"), {
            $addToSet : {
                "players." + Session.get("activePlayer") + ".hexIds" : Session.get("selected_hex")
            }
        });
以方法回答

Meteor.methods({
  addHexIds : function (hexIds, player, game) {
    Games.update({_id:game, "players.id" : player}, {
            $addToSet : {
                "players.$.hexIds" : hexIds
            }
        });
  }
});
也许像这样

Games.update(
    { board : hex_board(7), "players.id": 0 },
    {
        "$addToSet": { "players.$.hexIds": "foo" }
    }
)

您可以根据需要进行更改

就是这样!但是,您需要在服务器端通过它,并在客户机中调用它,因为您无法使用_id和其他字段在客户机上进行更新。Meteor.methods({addHexIds:function(hexIds,player,game){Games.update({{u-id:game,“players.id”:player},{$addToSet:{players.$.hexIds:hexIds}}});