I have a collection of Posts
and Users
where users can upvote/downvote each post. What would be the best way to store this in a mongodb database to make sure that users cannot vote for a given document more than once?
The simplest nosql-ish solution I came up with is storing an array of user_ids who voted inside each Post
document (or even array of (user_id, vote)
where vote
is +1 or -1 so that users will be able to change their vote). Is it a good idea from the performance standpoint given that each post can have thousands of votes?
What about really popular websites like Reddit where top posts can have hundreds of thousands of votes?
MongoDB documents are currently restricted to a maximum of 16MB, so assuming Gilbert's calculations are accurate, you wouldn't be able to store all 6 Million user_id
s in the Post
document.
However, you could consider storing the votes in the User
document instead (i.e. the post_id
s that the particular user voted for). It is much less likely that a user votes on 6 million different posts, so this way you won't reach the size limit as quickly.
Another way to handle this: If you expect that many votes for a particular post, you may want to store the votes outside the Post
documents in a separate collection and do an additional query, similar to a many-to-many JOIN table in SQL fashion:
user_votes { user_id: ObjectId(...), post_id: ObjectId(...), vote:-1 }
and create a compound index on (user_id, post_id).