A general way of tracking vote counts in general will be to save the number of votes in the mail document and update it atomically when a new value is clicked on an array of votes.
Since this is one update, you are guaranteed that the counter will correspond to the number of elements in the array.
If the number of aggregates is fixed and the site is very busy, you can expand this paradigm and increase additional counters, for example, one month, day and hour, but this can get out of control very quickly. So instead, you can use the new Aggregation Framework (available in version 2.1.2 dev, will be released in version 2.2). than Map / Reduce, and this will allow you to make the calculations that you want very simply, especially if you take care of saving the voting dates as an ISODate () type.
A typical pipeline for aggregation request for top recipients this month might look something like this:
today = new Date(); thisMonth = new Date(today.getFullYear(),today.getMonth()); thisMonthEnd = new Date(today.getFullYear(),today.getMonth()+1); db.posts.aggregate( [ {$match: { "Votes.votedate": {$gte:thisMonth, $lt:thisMonthEnd} } }, {$unwind: "$Votes" }, {$match: { "Votes.votedate": {$gte:thisMonth, $lt:thisMonthEnd} } }, {$group: { _id: "$title", votes: {$sum:1} } }, {$sort: {"votes": -1} }, {$limit: 10} ] );
This restricts the input to the pipeline to messages that have votes, comparing the voting dates with the month you are counting, unwinds the array to get one document per vote, and then makes the βgroupβ equivalent to sum all votes for each heading (I assume that the name is unique). Then he sorts by the number of votes and limits the output to the top ten.
You also have the opportunity to summarize the votes for the day (for example) for this month to find out which days are the most active for voting:
db.posts.aggregate( [ {$match: { "Votes.votedate": {$gte:thisMonth, $lt:thisMonthEnd} } }, {$unwind: "$Votes" }, {$match: { "Votes.votedate": {$gte:thisMonth, $lt:thisMonthEnd} } }, {$project: { "day" : { "$dayOfMonth" : "$Votes.votedate" } } }, {$group: { _id: "$day", votes: {$sum:1} } }, {$sort: {"votes": -1} }, {$limit: 10} ] );