Skip to content

Instantly share code, notes, and snippets.

@gurdotan
Created April 16, 2015 09:37
Show Gist options
  • Save gurdotan/47e23a480348a02b754c to your computer and use it in GitHub Desktop.
Save gurdotan/47e23a480348a02b754c to your computer and use it in GitHub Desktop.
MongoDB aggregation pipeline: group by date (ISO format)
db.users.aggregate(
{
"$match": {
created_at: { $gte: ISODate("2014-09-01T00:00:00Z") }
}
},
{
"$project": {
_id: 0,
"isoDate": { $substr: [ "$created_at", 0, 10 ] }
}
},
{
"$group": { "_id": "$isoDate", "count": { "$sum": 1 } }
},
{
$sort: { "_id": 1 }
}
).result.forEach(function(doc) {
print(doc._id + "," + doc.count);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment