mongoose.js: encontrar entradas de datos por día

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.

¿Cómo haces eso?

preguntado el 31 de julio de 12 a las 14:07

See the Cookbook article on that subject: -

1 Respuestas


First read up on mongo query syntax for dates, here is a how-to for date ranges:

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 momento.js won't have a problem with that date string.

Oh, I forgot the last bit of magic: Put that query into a método estático attached to your Event model, so from now on you can just call

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

Respondido 31 Jul 12, 15:07

Thank you for your insights. As far as I understand your code you will retrieve every event that started before a given date. So if I want to get everything at single day should I do a .gte("24-12-2012 00:00").lte("24-12-2012 23:59")? Also what about events that started the day before but ended on that day? - Sven

Hey there. Correct, yes and for the last case: just use ...where('endDate).lte(yourDate). :) - rdrey

One correction for your comment above: lte y gte probably won't work with strings such as your "24-12-2012 00:00", make sure you parse them into Javascript dates first! - rdrey

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.