我有这个架构猫鼬:MongoDB的查询阵列位置操作怪异的行为
var schema = mongoose.Schema({
game: Number,
words: Number,
players: [{
name: String,
wordPercentage: Number,
totalWords: Number
}]
});
而我想要做的是更新的阵列中的每个指标的wordPercentage做wordPercentage: (totalWords/totalGameWords) * 100
我有尝试: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
简直妙不可言,但那不是我有什么遗憾的了。
可爱,我一直用的回调,我看他们是如何更好的,谢谢! – MrSandman
不客气:) –
哦,我也做了一些小修改以适合我的需要,将schema.find改为'schema.findOne',并将'totalWords'改为我的局部变量,而不是如何在那里创建,再次,谢谢! :) – MrSandman