juanp_1982
juanp_1982

Reputation: 1007

How to make a query using dates

I need to pull from the DB all rows in a day

var i_sDate = "2014-06-21"; // (user input)
var startDate = new Date();
var month = parseInt(i_sDate.substr(5,2)) - 1;
var day = i_sDate.substr(8,2);
startDate.setFullYear(i_sDate.substr(0,4), month, day);
startDate.setHours(0, 0, 0, 0);

var endDate = new Date();
endDate.setFullYear(i_sDate.substr(0,4), month, day);
endDate.setHours(23, 59, 59, 0);

var query = {start_time:{"$gte": "ISODate('" + startDate.toISOString() + "')", "$lt": "ISODate('" + endDate.toISOString() + "')"}};
var tableInfo = Users_Collection.find(query).fetch();

console.log(query);

when I print "query" it looks OK, but I don't get any result at all, I put the same information directly on the DB and I get the expected result. It seems like I'm building the query in the wrong way, any suggestion?????

thanks in advance!

Upvotes: 0

Views: 116

Answers (1)

David Weldon
David Weldon

Reputation: 64312

You should directly use Date objects in your query. Try this:

var query = {start_time: {$gte: startDate, $lt: endDate}};

It also looks like you were missing a closing }.

Upvotes: 2

Related Questions