obj63mc
11/15/2017 - 4:37 PM

Aggregating Votes on mongodb collection

Aggregating Votes on mongodb collection

#Schema

{
  _id:....
  votes:[...]
}

#Aggregation

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}
]);