The new $dateToParts operator simplifies having multiple single date value extraction operators into a single one. Its syntax is:
The "timezone" argument is optional, and must evaluate to specify an Olson Timezone Identifier such as "Europe/London" or "America/New_York", or, a UTC offset string in the forms: "+03", "-0530", and "+04:45". If you specify a timezone argument it means that the date that you provided will be interpreted as it was in the given time zone.
The result of the operator is a sub-document with the broken down parts, expressed in the (optionally) given time zone:
$dateToParts also supports a third boolean argument, iso8601. If set to true, instead of year, month, and day, it returns the ISO 8601 isoYear, isoWeekYear, and isoDayOfWeek fields representing an ISO Week Date. With the same date, the example is represented as:
The $dateFromParts operator does the opposite of the $dateToParts operator and constructs a new Date value from its constituent parts, with the possibility of interpreting the given values in a different time zone.
Its syntax is either::
Each argument's expression needs to evaluate to a number. This means the source can be either double, NumberInt, NumberLong, or Decimal. Decimal and double values are only supported if they convert to a NumberLong without any data loss.
- Before MongoDB 3.5.12, the argument millisecond to dateFromParts is incorrectly spelled milliseconds.
- Until SERVER-30547 is resolved, $dateFromParts does not accept an sub-document as argument, and instead requires each single field to be specified.
SERVER-30523is resolved, the field values to dateFromParts can not underflow or overflow their expected range. For example, the day field's value needs to be in the range 1..31 and the hour field's value needs to be in the range 0..23.