A long time ago, I wrote about how to convert an ISODate() that's got time into just a date (meaning zeroing out the hours/minutes/seconds/milliseconds). We did it by subtracting from the date the number of milliseconds since midnight which we calculated using some date math and $hour, $minute, $second and $millisecond expressions.
As of 3.6 there's a slightly simpler way to achieve the same thing using the "$dateFromParts" expression.
​Here's an example:
As of 3.6 there's a slightly simpler way to achieve the same thing using the "$dateFromParts" expression.
​Here's an example:
> db.dates.find({},{_id:0})
{ "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
{ "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
{ "sys_created_on" : ISODate("2012-02-18T03:04:49Z") }
{ "sys_created_on" : ISODate("2012-02-10T03:04:49Z") }
{ "sys_created_on" : ISODate("2012-02-28T03:04:49Z") }
{ "sys_created_on" : ISODate("2012-03-18T03:04:49Z") }
> db.dates.aggregate({$project:{_id:0, roundDate:{$dateFromParts:{
year:{$year:"$sys_created_on"},
month:{$month:"$sys_created_on"},
day:{$dayOfMonth:"$sys_created_on"}
}}}})
{ "roundDate" : ISODate("2012-02-18T00:00:00Z") }
{ "roundDate" : ISODate("2012-02-18T00:00:00Z") }
{ "roundDate" : ISODate("2012-02-18T00:00:00Z") }
{ "roundDate" : ISODate("2012-02-10T00:00:00Z") }
{ "roundDate" : ISODate("2012-02-28T00:00:00Z") }
{ "roundDate" : ISODate("2012-03-18T00:00:00Z") }
RSS Feed