【发布时间】:2018-03-15 21:30:27
【问题描述】:
我有这个猫鼬模式:
var schema = mongoose.Schema({
game: Number,
words: Number,
players: [{
name: String,
wordPercentage: Number,
totalWords: Number
}]
});
而我想做的是通过wordPercentage: (totalWords / totalGameWords) * 100更新数组中每个索引的wordPercentage
我试过:db.gamedatas.findOneAndUpdate({game: 1}, {$set: { 'players.$.wordPercentage': (('players.$.totalWords' / totalGameWords) * 100 } }); 和 totalGameWords 是一个局部变量。
但我收到此错误:The positional operator did not find the match needed from the query. Unexpanded update: players.$.wordPercentage
我厌倦了使用players.$[].wordPercentage,但随后又出现另一个错误:cannot use the part (players of players.$[].wordPercentage)
使用players.1.wordPercentage 就像一种魅力,但遗憾的是,这不是我所追求的。
【问题讨论】: