//1. User
var UserSchema = new mongoose.Schema({
username: {type: String, lowercase: true},
email: {type: String, lowercase: true}
});
//2. Pool
var PoolSchema = new mongoose.Schema({
rating: {type: Number, default: 0},
votedNumber: {type: Number, default: 0}
});
//3. Voted
var VotedSchema = new mongoose.Schema({
pool: {type: mongoose.Schema.Types.ObjectId, ref: 'Pool'}
user: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
rank: Number, //rank could be -1 and 1 (-1 is no and 1 is yes) or 0-5
updatedAt: Date
});
Если пользователь проголосовал уже этот пул можно обновить объект и Проголосовано объект Pool. Но обычно пользователю разрешено голосовать один раз.
Кроме того, для расчета рейтинга для пула вам не нужно выбирать все голосовые документы из базы данных. Можно пересчитать его в 1 или 2 запросах к базе данных:
if (('undefined' != typeof pool.votedNumber && pool.votedNumber) ||
('undefined' != typeof pool.rating && pool.rating)) {
//Calculate new rating for a pool
var numNewRating = ((pool.rating * pool.votedNumber) + newRank)/(pool.votedNumber + 1);
place.votedNumber += 1;
place.rating = parseFloat(numNewRating);
} else {
//Pool is the first time ranked
place.rating = parseFloat(newRank);
place.votedNumber = 1;
}