0

I'm trying to get some documents from collection located between two dates(with 5 hour difference for example). My code:

Session.setDefault('sortBy', 'time');
    Template.content.helpers({
        posts: function() {
            if( Session.equals('sortBy', 'time') ){
                return Posts.find({author: Meteor.user().username}, {sort: {date_created: -1}});
            } 
            else if( Session.equals('sortBy', 'hours') ) {
                var endDate = new Date();
                var startDate = new Date(endDate);
                startDate.setHours(endDate.getHours()-5);
                console.log("startDate: " + startDate);
                console.log("endDate: " + endDate);

                return Posts.find({author: Meteor.user().username}, {date_created: {$gte: startDate,$lt: endDate}})
            }
        }
    });
    Template.content.events({
        'click .lasthour': function(e) {
            return Session.set('sortBy', 'hours');
        },
        'click .nosort': function(e) {
            return Session.set('sortBy', 'nosort');
        }
    });

But it always returns all documents (so filter not working). And problem not in Session variable 'sortBy', it working fine.

bartezr
  • 727
  • 3
  • 10
  • 26
  • There is no constructor new Date(Date) in Javascript. I would change var startDate = new Date(endDate); into var startDate = new Date(endDate.getTime()); or simply var startDate = new Date(); – mwarren Jul 18 '15 at 19:47
  • But console.log("startDate: " + startDate); returns right results – bartezr Jul 18 '15 at 19:51
  • Ok. Was Jordan Speizers answer no good? What is in the database? Are you sure that the date_created field comtains a date- in other words it's not a string field? – mwarren Jul 18 '15 at 20:05
  • Have you tried the date_created part of the query on its own without the author field part? – mwarren Jul 18 '15 at 20:11
  • Firstly, console.log(startDate) looks like (format) Fri Mar 01 1901 00:00:00 GMT+0300 (Russian Standard Time) If i'm removing author from query, result is empty – bartezr Jul 18 '15 at 20:39
  • March 1st 1901?? And whats endDste? – mwarren Jul 18 '15 at 21:00
  • I gave you example date, real dates are today, and they are right – bartezr Jul 18 '15 at 21:07
  • I'd be grateful if you would show exact console.logs for both start and end dates. – mwarren Jul 18 '15 at 21:45
  • http://stackoverflow.com/questions/2943222/find-objects-between-two-dates-mongodb. Take a look at this. They reckon that you have to convert to ISO dates. There are a lot of stackoverflow questions about searching between dates. – mwarren Jul 18 '15 at 21:50
  • Problem resolved. I'm have been saving dates in unix format, now i'm replacing date_created: Date.now() in create.js to date_created: new Date() – bartezr Jul 19 '15 at 06:32

1 Answers1

1

Your query is formatted wrong. Should look like this:

return Posts.find(
  {
    author: Meteor.user().username,
    date_created: {$gte: startDate, $lt: endDate}
  }
);
Jordan Speizer
  • 573
  • 4
  • 11