adrianmcli
adrianmcli

Reputation: 1996

In Meteor, how do I find a record in a collection given the year, month, day?

So I have a collection of posts where a JavaScript Date object is stored in the "submitted" field. Given the year/month/day (2015/02/03 for example), I need to be able to pull the records that fit this description.

I tried something like this, but it didn't work. I'm clueless as to the correct syntax:

Posts.find({$where : 'return this.submitted.getMonth() == 2 && this.submitted.getDay() == 3 && this.submitted.getYear() == 2015'})

Also, is it better for me to just separately store 3 separate variables in the collection to begin with? Like submitted.year, submitted.month, submitted.day. It sounds a lot simpler, but it requires me to add in a whole bunch of fields, which may seem redundant.

Upvotes: 0

Views: 143

Answers (1)

Dan Dascalescu
Dan Dascalescu

Reputation: 152125

This is a question. The solution is to search within a date range. Tweak the query below:

Posts.find({submitted: {$gte: new Date('2015-02-23'), $lt: new Date('2015-02-04')}})

Upvotes: 2

Related Questions