MongoDB comparing dates only without times
I guess You should make a range query between the start of the day and its ending(or without ending if you are talking about today). Something like this
db.collection.find({
"date" : {"$gte": new Date("2015-07-07T00:00:00.000Z"),
"$lt": new Date("2015-07-08T00:00:00.000Z")}
})
Updated 2018-06-26 fixed up the code to use moment() instead of new Date()
I solved this by use MomentJS Timezone (http://momentjs.com/timezone/) to convert a local date/time to a date-only numerical field, then I store the date as a number.
In my javascript code (outside of MongoDB):
var localDateOnly = function(timezone, d) {
if (d == undefined) { d = new Date(); } // current date/time
return Number( moment(d).tz(timezone).format("YYYYMMDD") );
}
Then I store a date-only field in the Mongo record.
var myDate = localDateOnly("America/New_York"); // today, in YYYYMMDD number
db.birthdays.insert(
{ dateonly: myDate, event: "This day is my birthday!" }
);
Then in my Javascript code, I can easily query today, tomorrow, specific days, etc.
// today
var myDate = localDateOnly("America/New_York");
db.birthdays.find( { dateonly: myDate } );
// tomorrow
var myDate = localDateOnly(
"America/New_York",
moment().add( 1, "days" )
); // tomorrow
db.birthdays.find( { dateonly: myDate } );
// someone wants to know birthdays on the calendar on July 15, 2015,
// no timezone math needed, just type the date in YYYYMMDD format
db.birthdays.find( { dateonly: 20150715 } );
Hope this helps someone. Decided to store as a number for performance and validity checking. Before insert, I check for a valid date using the moment package as well:
moment( 20150715, "YYYYMMDD", true ).isValid() // returns true & allowed to store in the database
moment( 150715, "YYYYMMDD", true ).isValid() // returns false, don't insert into db
Code was cleaner when storing integers that represented dates, and made it easy for mongo to find ranges. Like, find birthdays in 2015 would be {"$gte": 20150101, "$lte": 20151231}
or {"$gte": 20150000, "$lte": 20159999}
.
It's possible with $expr
{ $expr: {$eq: ["2021-03-29", { $dateToString: {date: "$dateField", format: "%Y-%m-%d"}}]}}
You can extract the date as string at any given format and compare it with your date at that format using aggregation pipiline
$addFields: { "creationDate": {$dateToString:{format: "%Y-%m-%d", date: "$createdAt"}}}},
{$match : { creationDate: {$eq: req.query.date}}