我有以下代码可行。但我希望它更具动态性,而不是“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();
}
答案 0 :(得分:0)
看起来好像你想要这个:
var value = {};
value['mPlayers.' + counter] = updatedPlayer;
models.GameBoard.findOneAndUpdate({_id: game._id}, {$set: value}).exec();