I'm trying to model a voting system in MongoDB. You could imagine it as a voting system similar to reddit. Requirements:
I see two approaches here (correct me if I'm wrong!):
I've also played with the idea of embedding votes into 'buckets' grouped by hour in a separate collection.
No. 1 would be very fast for requirement No. 2 but I don't know if requirement No. 3 is even possible in this scenario.
No. 2 would be slower for requirement No. 2 and I'm not sure what the performance would be like for requirement No. 3 / how it would be achieved (map reduce?).
Basically it seems like I need to start with a reasonably fast solution for requirement No. 3, and then make sure that requirement No 2 is not too slow. Ideas?
Use embedded method. Add a parameter to each object for hourly-score, daily-score, monthly-score, etc. Add another boolean parameter recently-voted, recent-hourly, and recent-daily. Create a script that runs a map-reduce on objects to calculate and update these parameters.
The script would be run via cron in three variations.
The idea is to minimize unnecessary processing on objects that aren't relevant to the score calculation script being run (hourly should only be run on objects that have been voted on since the last time hourly was run, or objects that have not been voted on and need to be reset to 0). Another nice benefit is the *-score values don't just have to be calculated based on the object votes. You could include page views for example, or whatever. Thoughts on this approach?
Check out the Voting with Atomic Operators recipe in the Mongo Cookbook: http://cookbook.mongodb.org/patterns/votes/. It doesn't tell you how to implement aggregation, but you could perhaps do that by making stand-in objects that represent the objects to vote on, but for a specific time period.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With