简体   繁体   中英

Limit one vote per day in MERN stack appliction

I am using the MongoDB database and node.js as the backend. I want to implement a voting system where a user after logging in to my website and can vote on certain things. But I want to make sure that they can make only one vote per day. How can I implement this?

A possible solution that I thought of:-

Store the latest vote timestamp and then check when he tries to vote again weather a day has passed or not. But this is not perfect as it means there should be a gap of 24 hrs between two votes irrespective of the time of the day he votes in.

Could someone suggest something better than I can implement in node.js and MongoDB?

Thanks in advance.

Id use Redis with ttl for this and on a middleware I would check if the user already voted(exists in the Redis). But if you dont use Redis you can also use TTL on mongoDB, so Id create a schema for this and check on every request in middleware.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM