Skip to content

Instantly share code, notes, and snippets.

@danielskapunk
Created November 3, 2017 20:25
Show Gist options
  • Select an option

  • Save danielskapunk/257d03c000e2c08f48cfec9b56be8633 to your computer and use it in GitHub Desktop.

Select an option

Save danielskapunk/257d03c000e2c08f48cfec9b56be8633 to your computer and use it in GitHub Desktop.
Mongo Group Aggregate
//GROUP BY WORKSHOP ID
db.getCollection('enrolments').aggregate([{
$group:
{
_id: {workshopId: "$workshopId"},
contacts: { $push: {contactId: '$contactId'} }
}
}])
GROUP FIRST BY WEEKDAY AND WORKSHOP THEN BY WEEKDAY
db.getCollection('enrolments').aggregate([
{ $unwind: '$attendances' },
{
$group:
{
_id: {weekDay: "$attendances.weekDay", workshopId:"$workshopId"},
contacts: { $push: {contactId: '$contactId', day: "$attendances.day", status:'$attendances.status'} }
}
},
{
$group:
{
_id: {weekDay: "$_id.weekDay"},
workshops: { $push: {workshopId:'$_id.workshopId', contacts: '$contacts'} }
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment