Context:
Okay so I am making a polling website. There are basically two ways to vote. Simple vote which just counts the number of votes for a movie. And ranked voting where each user ranks each movie and then the winner is decided using instant runoff. Here is my idea of how to handle it: I have a poll collection which just stores the rank for each movie for ranked polls and number of votes for each movie for simple. So something like this:
{
_id: ObjectId
pollType: string
movies: array
0: Object
id: ObjectId
title: String
rank: number
votes: number
}
Then I have a vote collection which a vote is added each time a user votes.
{
_id:ObjectId
pollId: Objectid
votedMovies: Array
0: Object
id: ObjectId
title: string
rank: number
}
When the user votes I will then retrieve all the votes for a poll and for simple votes increment the vote count based on what movies where in the votedMovies Array. For ranked voting I would get each movie rank for every vote and then do a instant runoff calculation. Finally I would then update the polls votes and rankings for each movie.
Question:
So far I’m only worried about the simple vote. How would I update the number of votes for each movie on the poll collection? This is what I have so far:
async function updateVotes(id){
const cursor = db.collection('votes').find({poll: id});
console.log(id);
const pollid = new ObjectId(id);
console.log(poll);
await cursor.forEach(function(doc) {
const poll = await polls.updateOne({_id:pollid}, {$set: {"movies.$[]"}}); //problem line
});
}