Shamoon
Shamoon

Reputation: 43609

How can I query for dates with Node.js and Mongoose?

I have:

    dates = {}
    today = new Date()
    todayminus7days = new Date(today).setDate(today.getDate()-7)
    dates.startDate = todayminus7days
    dates.endDate = today

    query =
      person_gender: filter.gender if filter.gender
      person_age:
        0:
          $gte: dates.startDate
        1:
          $lte: dates.endDate

However, when I run this query through a Mongoose model, the end query is:

{ person_gender: 'female',
  person_age: 
   { '0': { '$gte': 1317055089524 },
     '1': { '$lte': Mon, 03 Oct 2011 16:38:09 GMT } } }

and this returns null results within that date range.

How can I pass Date's to Mongoose then?

Upvotes: 3

Views: 3618

Answers (1)

cjohn
cjohn

Reputation: 11660

Your problem here is not MongoDB or Mongoose related, but in your assumption that .setDate() returns a Date (which it doesn't).

If you change your initialization code to:

...
todayminus7days = new Date(today);
todayminus7days.setDate(-7);
...

It should work as expected.

Upvotes: 6

Related Questions