Skip to content

Instantly share code, notes, and snippets.

@glpunk
Last active December 31, 2015 20:58
Show Gist options
  • Save glpunk/8043266 to your computer and use it in GitHub Desktop.
Save glpunk/8043266 to your computer and use it in GitHub Desktop.
mongodb aggregate example. First query counts documents by country. Second, counts documents by date
db.contests_tbbt2013.aggregate(
{ $group: {_id: "$country", total: {$sum: 1} } }
);
db.contests_tbbt2013.aggregate(
{$group:
{
_id:{ $dayOfYear:'$regdate' },
sum:{$sum:1},
date:{$first: '$regdate'}
}
},
{ $sort : { date:1 } }
);
db.schedules.aggregate(
{$group:
{
_id:{ $dayOfYear:'$start' },
sum:{$sum:1},
date:{$first: '$start'}
}
},
{$match:{date: {$gte: new Date('jan 17,2014')}}},
{ $sort : { date:1 } },
{$limit: 15}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment