- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $dateFromParts (aggregation)
$dateFromParts (aggregation)¶
On this page
Definition¶
-
$dateFromParts¶ New in version 3.6.
Constructs and returns a Date object given the date’s constituent properties.
The
$dateFromPartsexpression has the following syntax:You can also specify your constituent date fields in ISO week date format using the following syntax:
The
$dateFromPartstakes a document with the following fields:Important
You cannot combine the use of calendar dates and ISO week date fields when constructing your
$dateFromPartsinput document.Field Required/Optional Description yearRequired if not using isoWeekYearCalendar year. Can be any expression that evaluates to a number.
Value range:
0-9999isoWeekYearRequired if not using yearISO Week Date Year. Can be any expression that evaluates to a number.
Value range:
0-9999monthOptional. Can only be used with year.Month. Can be any expression that evaluates to a number.
Defaults to
1.Value range:
1-12Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.isoWeekOptional. Can only be used with isoWeekYear.Week of year. Can be any expression that evaluates to a number.
Defaults to
1.Value range:
1-53Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.dayOptional. Can only be used with year.Day of month. Can be any expression that evaluates to a number.
Defaults to
1.Value range:
1-31Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.isoDayOfWeekOptional. Can only be used with isoWeekYear.Day of week (Monday
1- Sunday7). Can be any expression that evaluates to a number.Defaults to
1.Value range:
1-7Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.hourOptional Hour. Can be any expression that evaluates to a number.
Defaults to
0.Value range:
0-23Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.minuteOptional Minute. Can be any expression that evaluates to a number.
Defaults to
0.Value range:
0-59Starting in MongoDB 4.0, if the number specified is outside this range,$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.secondOptional Second. Can be any expression that evaluates to a number.
Defaults to
0.Value range:
0-59Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.millisecondOptional Millisecond. Can be any expression that evaluates to a number.
Defaults to
0.Value range:
0-999Starting in MongoDB 4.0, if the number specified is outside this range,
$dateFromPartsincorporates the difference in the date calculation. See Value Range for examples.timezoneOptional <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.
- an Olson Timezone Identifier,
such as
Behavior¶
Value Range¶
Starting in MongoDB 4.0, if the value specified for fields other than
year, isoYear, and timezone is outside the valid range,
$dateFromParts carries or subtracts the difference from
other date parts to calculate the date.
Value is Greater than the Range¶
Consider the following $dateFromParts expression where
the month field value is 14, which is 2 months greater than the
maximum value of 12 months(or 1 year):
The expression calculates the date by increasing the year by 1
and setting the month to 2 to return:
Value is Less than the Range¶
Consider the following $dateFromParts expression where
the month field value is 0, which is 1 month less than the
minimum value of 1 month:
The expression calculates the date by decreasing the year by 1
and setting the month to 12 to return:
Time Zone¶
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¶
The following aggregation uses $dateFromParts to
construct three date objects from the provided input fields:
The operation returns the following result: