Storing Votes/Likes/Claps/etc. in Database for Full-Stack Apps

Storing Votes/Likes/Claps/etc. in Database for Full-Stack Apps
0.0 0

#1

I recently finished the voting app and am now beginning planning for the nightlife coordination app. I’ve been using Mongoose for back-end database storage, and I had a general question on how to store things like votes/likes/claps/etc.

In the voting app, I wanted to make sure that a user could not vote twice. I did this by storing (in Mongoose) for each poll, an array consisting of usernames of who already voted. Then, when it was time to process a user clicking on “vote”, I took the username and compared it with the entire array of usernames who already voted. If I got through the array without a username match, then I would tack my username at the end of the array. If I got a username match within the array, then I would stop and return an error that I’m not allowed to vote.

This was fine for a small app, but I feel that when there are thousands of votes that this is not the best idea because I have to check through all of them before a user gets to vote. How should I organize things to solve this problem?

Thanks.


#2

You could store the user id instead of the username? Easier to index and would take up less storage space.
Computers & databases are designed to be searched quickly.

By the way, the method I used to stop people voting twice was to store something in local storage using the Poll id. This is not perfect as if they use a different browser or clear local storage they can vote again. Problem is that people can vote anonymously so difficult to track if the same user has voted before.


#3

Thanks, that does help a bit and I will use user ids rather than usernames. Still, I need to find a way to not have to do a linear search through the user ids. I want to make sure that even if you use a different browser or local storage is cleared that you can’t vote twice, which is of course a reasonable feature. I don’t want to have to test every user id one by one. I’m not sure how I can get Mongoose to help me with this.


#4

Well if you don’t want to use userids which are index and therefore a fast search, I would sort the users by username. In a relational DB this would be done with a view.
You can apply indexes in MongoDb.