I'm using Node.js and Express on a webserver to query MongoDB on a dbserver and running into a problem getting the dates to work together, I've read a bunch of threads here about the problem, most notably this one:
Inserting and Querying Date with MongoDB and Nodejs
I'm pretty sure I understand what I'm supposed to be doing, but nothing I try seems to work.
In my .js file, I have the following:
var todayStart = new Date();
todayStart.setSeconds(0);
todayStart.setHours(0);
todayStart.setMinutes(0);
todayStart.setMilliseconds(0);
var todayEnd = new Date(todayStart);
todayEnd.setHours(23);
todayEnd.setMinutes(59);
todayEnd.setSeconds(59);
todayEnd.setMilliseconds(999);
var query = {
"date": {
$gte: new Date(todayStart).toISOString(),
$lte: new Date(todayEnd).toISOString()
}
};
My query var outputs to the console in the format I expect:
{ date:
{ '$gte': '2016-09-13T00:00:00.000Z',
'$lte': '2016-09-13T23:59:59.999Z' }
}
I attempt to use either my query variable or the date vars themselves, and both return a result of null (no errors):
db.collection('test').findOne(query, function(err, result) {
db.collection('test').findOne({"date" : {$gte: new Date(todayStart).toISOString(),$lte: new Date(todayEnd).toISOString() }}, function(err, result) {
However if I just plug in my dates like so, it returns my result:
db.collection('test').findOne({"date" : {$gte: new Date("2016-09-13T00:00:00.000Z"),$lte: new Date("2016-09-13T23:59:59.999Z") }}, function(err, result) {
Any idea what I'm missing here?