On this page本页内容
$dateFromParts
¶New in version 3.6.版本3.6中的新功能。
Constructs and returns a Date object given the date’s constituent properties.根据日期的组成属性构造并返回日期对象。
The $dateFromParts
expression has the following syntax:$dateFromParts
表达式语法如下所示:
You can also specify your constituent date fields in ISO week date format using the following syntax:您还可以使用以下语法以ISO week date格式指定组成日期字段:
The $dateFromParts
takes a document with the following fields:$dateFromParts
采用带有以下字段的文档
Important
You cannot combine the use of calendar dates and ISO week date fields when constructing your 在构建$dateFromParts
input document.$dateFromParts
输入文档时,不能同时使用日历日期和ISO周日期字段。
year |
isoWeekYear isoWeekYear ,则需要 |
|
isoWeekYear |
year year ,则需要 |
|
month |
year .year 一起使用。 |
|
isoWeek |
isoWeekYear .isoWeekYear 一起使用。 |
|
day |
year .year 一起使用。 |
|
isoDayOfWeek |
isoWeekYear .isoWeekYear 一起使用。 |
Day of week (Monday
|
hour |
| |
minute |
| |
second |
| |
millisecond |
| |
timezone |
|
Starting in MongoDB 4.4, the supported value range for 从MongoDB 4.4开始,year
and isoWeekYear
is 1-9999
. year
和isoWeekYear
支持的值范围为1-9999
。In prior versions of MongoDB, the lower bound for these values was 在早期版本的MongoDB中,这些值的下限为0
and the supported value range was 0-9999
.0
,支持的值范围为0-9999
。
Starting in MongoDB 4.0, if the value specified for fields other than 从MongoDB 4.0开始,如果为year
, isoWeekYear
, and timezone
is outside the valid range, $dateFromParts
carries or subtracts the difference from other date parts to calculate the date.year
、isoWeekYear
和timezone
以外的字段指定的值超出有效范围,$dateFromParts
携带或减去与其他日期部分的差值以计算日期。
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):$dateFromParts
表达式,其中month
字段值为14
,比最大值为12个月(或1年)大2个月:
The expression calculates the date by increasing the 表达式通过将year
by 1 and setting the month
to 2 to return:year
增加1并将month
设置为2来计算日期,以返回:
Consider the following 考虑下面的$dateFromParts
expression where the month
field value is 0
, which is 1 month less than the minimum value of 1 month:$dateFromParts
表达式,其中month
字段值为0
,比1个月的最小值少1个月:
The expression calculates the date by decreasing the 表达式通过将year
by 1 and setting the month
to 12 to return:year
减少1并将month
设置为12来计算日期,以返回:
When using an Olson Timezone Identifier in the 在<timezone>
field, MongoDB applies the DST offset if applicable for the specified timezone.<Timezone>
字段中使用奥尔森时区标识符时,MongoDB会在适用于指定时区的情况下应用DST偏移量。
For example, consider a 例如,考虑一个sales
collection with the following document:sales
集合带有以下文档:
The following aggregation illustrates how MongoDB handles the DST offset for the Olson Timezone Identifier. 下面的聚合说明了MongoDB如何处理Olson时区标识符的DST偏移量。The example uses the 该示例使用$hour
and $minute
operators to return the corresponding portions of the date
field:$hour
和$minute
运算符返回日期字段的相应部分:
The operation returns the following result:操作返回以下结果:
The following aggregation uses 以下聚合使用$dateFromParts
to construct three date objects from the provided input fields:$dateFromParts
从提供的输入字段构造三个日期对象:
The operation returns the following result:操作返回以下结果: