mongodb – return query based on date
mongodb – return query based on date
You probably want to make a range query, for example, all items created after a given date:
db.gpsdatas.find({createdAt : { $gte : new ISODate(2012-01-12T20:15:31Z) }});
Im using $gte
(greater than or equals), because this is often used for date-only queries, where the time component is 00:00:00.
If you really want to find a date that equals another date, the syntax would be
db.gpsdatas.find({createdAt : new ISODate(2012-01-12T20:15:31Z) });
Just been implementing something similar in Mongo v3.2.3 using Node v0.12.7 and v4.4.4 and used:
{ $gte: new Date(dateVar).toISOString() }
Im passing in an ISODate (e.g. 2016-04-22T00:00:00Z) and this works for a .find() query with or without the toISOString function. But when using in an .aggregate() $match query it doesnt like the toISOString function!
mongodb – return query based on date
if you want to get items anywhere on that date you need to compare two dates
You can create two dates off of the first one like this, to get the start of the day, and the end of the day.
var startDate = new Date(); // this is the starting date that looks like ISODate(2014-10-03T04:00:00.188Z)
startDate.setSeconds(0);
startDate.setHours(0);
startDate.setMinutes(0);
var dateMidnight = new Date(startDate);
dateMidnight.setHours(23);
dateMidnight.setMinutes(59);
dateMidnight.setSeconds(59);
### MONGO QUERY
var query = {
inserted_at: {
$gt:morning,
$lt:dateScrapedMidnight
}
};
//MORNING: Sun Oct 12 2014 00:00:00 GMT-0400 (EDT)
//MIDNIGHT: Sun Oct 12 2014 23:59:59 GMT-0400 (EDT)