mongoose.js: finding data entries by day

Suppose this is your model:

var EventSchema = new Schema({
    title: String,
    startDate: Date,
    endDate: Date
});

I would like to throw in a date (as simple as like '24-12-2012') and then retrieve every event that started or proceeded at that single day.

How do you do that?

Sure:

First read up on mongo query syntax for dates, here is a how-to for date ranges: http://cookbook.mongodb.org/patterns/date_range/

Then translate that mongo syntax into mongoose.

Event.where('startDate').lte(yourDate).exec(callback); //should do the trick

Now you just need to parse date strings to javascript dates. There are some cool libraries to do that, I believe that moment.js won't have a problem with that date string.

Oh, I forgot the last bit of magic: Put that query into a static method attached to your Event model, so from now on you can just call

Event.earlierThan(yourDate, callback); //Where your custom static is called 'earlierThan'.