1
我有兩個Mongoose模式/模型。一個爲Team
的球員,另一個爲Players
本身。Mongo - 從幾個參考對象數組中取值的總和
隊:
// Other less relevant stuff
goalkeepers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
}],
defenders: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
}],
midfielders: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
}],
attackers: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Player'
}],
points: Number
球員:
// Other less relevant stuff
points: Number
的問題是,我需要的Team
點是Players
的所有點的總和每個位置陣列。
我目前正在研究的領域是MongoDB聚合函數,特別是$sum
,但沒有一個示例包含ref
對象。這些對象也需要首先填充,以便可以讀取點值。
我覺得以前肯定有人遇到過這個問題,但我的搜索很遺憾。
非常感謝!我只想說$ lookup需要更新版本的MongoDB。 3.2+我相信。這不適用於2.6.2。 – Harry