I read the Voting with Atomic Operators article. In this article, the voters
field is an array of ObjectId
s. But I would like to store the voters
as an array of embedded documents with the following format:
{
user: ObjectId,
date: Date,
isDownvote: Boolean, // If false, it's an upvote.
}
The user can upvote or downvote the post just like the voting system provided by Stack Overflow. So, for example, when the user want to upvote a post, there are cases to consider:
- If a downvote by the user already exists, then update the vote's
isDownvote
tofalse
. - Else push a new vote with
isDownvote
beingfalse
.
How can I push/pull a vote in single query with this format of votes?