- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $dateToParts (aggregation)
$dateToParts (aggregation)¶
On this page
Definition¶
-
$dateToParts¶ New in version 3.6.
Returns a document that contains the constituent parts of a given BSON Date value as individual properties. The properties returned are
year,month,day,hour,minute,secondandmillisecond.You can set the
iso8601property totrueto return the parts representing an ISO week date instead. This will return a document where the properties areisoWeekYear,isoWeek,isoDayOfWeek,hour,minute,secondandmillisecond.The
$dateToPartsexpression has the following syntax:The
$dateToPartstakes a document with the following fields:Field Required/Optional Description dateRequired Changed in version 3.6.
The input date for which to return parts.
<dateExpression>can be any expression that resolves to a Date, a Timestamp, or an ObjectID. For more information on expressions, see Expressions.timezoneOptional The timezone to use to format the date. By default,
$dateToPartsuses UTC.<timezone>can be any expression that evaluates to a string whose value is either:- an Olson Timezone Identifier,
such as
"Europe/London"or"America/New_York", or - a UTC offset in the form:
+/-[hh]:[mm], e.g."+04:45", or+/-[hh][mm], e.g."-0530", or+/-[hh], e.g."+03".
For more information on expressions, see Expressions.
iso8601Optional If set to true, modifies the output document to use ISO week date fields. Defaults tofalse.- an Olson Timezone Identifier,
such as
Behavior¶
When using an Olson Timezone Identifier in the <timezone>
field, MongoDB applies the DST offset
if applicable for the specified timezone.
For example, consider a sales collection with the following document:
The following aggregation illustrates how MongoDB handles the DST
offset for the Olson Timezone Identifier. The example uses the
$hour and $minute operators to return the
corresponding portions of the date field:
The operation returns the following result:
Example¶
Consider a sales collection with the following document:
The following aggregation uses $dateToParts to return a
document that contains the constituent parts of the date field.
The operation returns the following result: