简体   繁体   中英

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.

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')}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM