【发布时间】:2020-08-10 22:01:46
【问题描述】:
我对 MongoDB 和 Mongoose 非常陌生,似乎发现自己需要一个相当复杂的查询。我的架构如下。
const gameSchema = new mongoose.Schema({
_winnerId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
_loserId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
winnerWords: [String],
loserWords: [String],
});
我正在尝试获取给定用户 ID 的赢/输比率。不知何故,我需要计算用户 ID 作为 _winnerId 出现的次数以及用户 ID 作为 _loserId 出现的次数。这可以通过单个查询来完成吗?
【问题讨论】: