sqram
sqram

Reputation: 7201

Grouping & sorting results by date in Mongoose

Suppose my users documents store the date they signed up, in the format of

date : {type:Date, default: Date.now}

So dates are stored like

Tue Jan 14 2014 23:34:02 GMT-0500 (Eastern Standard Time)

Say I want to get all users who signed up in January only. Is it possible with Mongoose by having single date field (and if so, is it possible to sort them by, say, day), or should i create a day/month/year field in the Schema?

Upvotes: 0

Views: 256

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312149

Create Date objects that span the month of January and then use them to filter on date:

Users.find({date: {$gte: new Date(2014, 0, 1), $lt: new Date(2014, 1, 1)}})
    .sort('date').exec(callback);

Upvotes: 1

Related Questions