Skip to content

Instantly share code, notes, and snippets.

@guilleferrer
guilleferrer / Date Range Mongo Query
Created January 24, 2012 11:50
Query for a date range using Mongo Timestamps
db.Collection.find({
created_at : {
'$gte': new Timestamp(new Date(2012, 0, 21), 0),
'$lte': new Timestamp(new Date(2012, 0, 22), 0)
})
ranges: {
'Today': ['today', 'today'],
'Yesterday': ['yesterday', 'yesterday'],
'Last 7 Days': [Date.today().add({
days: -6
}), 'today'],
'Last 30 Days': [Date.today().add({
days: -29
}), 'today'],
'This Month': [Date.today().moveToFirstDayOfMonth(), Date.today().moveToLastDayOfMonth()],