Reputation: 189
I have a Topics collection. Each Topic can be Rated. I denormalized the data so that the Topics collection stores the time of the most recent rating in "lastRated".
How do I query for all Topics that have not been rated in the last 6 hours?
My query right now looks like this:
var hoursAgo = 1000 * 60 * 60 * 24;
var beHereNow = new Date() - hoursAgo;
Topics.find( { 'lastRating' : {$lte: beHereNow} } );
However, when I run this in the console, I'm retrieving all the Topics.
Upvotes: 0
Views: 29
Reputation: 103425
Try setting the hours this way:
var date = new Date();
var beHereNow = new Date();
beHereNow.setHours(date.getHours() - 6);
console.log(beHereNow); // Fri May 08 2015 10:09:32 GMT+0100 (GMT Daylight Time)
Topics.find({ 'lastRating' : {$lte: beHereNow} });
Or better yet, you could use the momentjs library, especially the subtract()
method:
var beHereNow = moment().subtract(6, 'hours');
Topics.find({ 'lastRating' : {$lte: beHereNow} });
Upvotes: 2