0

我有以下有效的代码。但我希望它更加动态,而不是“mPlayers.0”,我希望它是“mPlayers.counter”,这样我就可以有一个查询。这似乎不是一件很难的事情,但我就是无法让它发挥作用。

任何帮助表示赞赏!

if(counter === 0) {
  models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: {"mPlayers.0": updatedPlayer}}).exec();
} else if(counter === 1) {
  models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: {"mPlayers.1": updatedPlayer}}).exec();
} else if(counter === 2) {
  models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: {"mPlayers.2": updatedPlayer}}).exec();
} else if(counter === 3) {
  models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: {"mPlayers.3": updatedPlayer}}).exec();
}
4

1 回答 1

0

看起来好像你想要这个:

var value = {};
value['mPlayers.' + counter] = updatedPlayer;
models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: value}).exec();
于 2013-03-17T12:11:45.687 回答