Docs Menu

$dateFromParts (집계)

이 페이지의 내용

$dateFromParts

Constructs and returns a Date object given the date's constituent properties.

$dateFromParts 표현식의 구문은 다음과 같습니다.

{
$dateFromParts : {
'year': <year>, 'month': <month>, 'day': <day>,
'hour': <hour>, 'minute': <minute>, 'second': <second>,
'millisecond': <ms>, 'timezone': <tzExpression>
}
}

You can also specify your constituent date fields in ISO week date format using the following syntax:

{
$dateFromParts : {
'isoWeekYear': <year>, 'isoWeek': <week>, 'isoDayOfWeek': <day>,
'hour': <hour>, 'minute': <minute>, 'second': <second>,
'millisecond': <ms>, 'timezone': <tzExpression>
}
}

$dateFromParts는 다음 필드가 포함된 문서를 사용합니다.

중요

You cannot combine the use of calendar dates and ISO week date fields when constructing your $dateFromParts input document.

필드
필수/선택 사항
설명

year

Required if not using isoWeekYear

Calendar year. Can be any 표현식 that evaluates to a number.

Value range: 1-9999

If the number specified is outside this range, $dateFromParts errors. The lower bound for this value is 1.

isoWeekYear

Required if not using year

ISO Week Date Year. Can be any 표현식 that evaluates to a number.

Value range: 1-9999

If the number specified is outside this range, $dateFromParts errors. The lower bound for this value is 1.

month

Optional. Can only be used with year.

Month. Can be any 표현식 that evaluates to a number.

기본값은 1입니다.

Value range: 1-12

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

isoWeek

Optional. Can only be used with isoWeekYear.

Week of year. Can be any 표현식 that evaluates to a number.

기본값은 1입니다.

Value range: 1-53

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

day

Optional. Can only be used with year.

Day of month. Can be any 표현식 that evaluates to a number.

기본값은 1입니다.

Value range: 1-31

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

isoDayOfWeek

Optional. Can only be used with isoWeekYear.

Day of week (Monday 1 - Sunday 7). Can be any 표현식 that evaluates to a number.

기본값은 1입니다.

Value range: 1-7

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

hour

옵션

Hour. Can be any 표현식 that evaluates to a number.

기본값은 0입니다.

Value range: 0-23

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

minute

옵션

Minute. Can be any 표현식 that evaluates to a number.

기본값은 0입니다.

Value range: 0-59 If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

second

옵션

Second. Can be any 표현식 that evaluates to a number.

기본값은 0입니다.

Value range: 0-59

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

millisecond

옵션

Millisecond. Can be any 표현식 that evaluates to a number.

기본값은 0입니다.

Value range: 0-999

If the number specified is outside this range, $dateFromParts incorporates the difference in the date calculation. See Value Range for examples.

timezone

옵션

<timezone> 은 값이 다음의 둘 중 하나인 문자열로 평가되는 모든 표현식이 될 수 있습니다.

  • Olson Timezone Identifier(예: "Europe/London"또는 "America/New_York") 또는

  • 형식의 UTC 오프셋입니다.

    • +/-[hh]:[mm](예: "+04:45") 또는

    • +/-[hh][mm](예: "-0530") 또는

    • +/-[hh], e.g. "+03".

표현식에 대한 자세한 내용은 표현식을 참조하세요 .

The supported value range for year and isoWeekYear is 1-9999.

If the value specified for fields other than year, isoWeekYear, and timezone is outside the valid range, $dateFromParts carries or subtracts the difference from other date parts to calculate the date.

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: { 'year' : 2017, 'month' : 14, 'day': 1, 'hour' : 12 } }

The expression calculates the date by increasing the year by 1 and setting the month to 2 to return:

ISODate("2018-02-01T12:00:00Z")

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: { 'year' : 2017, 'month' : 0, 'day': 1, 'hour' : 12 } }

The expression calculates the date by decreasing the year by 1 and setting the month to 12 to return:

ISODate("2016-12-01T12:00:00Z")

<timezone> 필드에서 Olson 표준 시간대 ID를 사용할 때 MongoDB는 지정된 시간대에 적용 가능한 경우 DST 오프셋을 적용합니다.

예를 들어 sales 다음 문서가 있는 컬렉션을 생각해 보겠습니다.

{
"_id" : 1,
"item" : "abc",
"price" : 20,
"quantity" : 5,
"date" : ISODate("2017-05-20T10:24:51.303Z")
}

다음 집계는 MongoDB가 Olson 시간대 식별자에 대한 DST 오프셋을 처리하는 방법을 보여 줍니다. 이 예시에서는 $hour$minute 연산자를 사용하여 date 필드의 해당 부분을 반환합니다.

db.sales.aggregate([
{
$project: {
"nycHour": {
$hour: { date: "$date", timezone: "-05:00" }
},
"nycMinute": {
$minute: { date: "$date", timezone: "-05:00" }
},
"gmtHour": {
$hour: { date: "$date", timezone: "GMT" }
},
"gmtMinute": {
$minute: { date: "$date", timezone: "GMT" } },
"nycOlsonHour": {
$hour: { date: "$date", timezone: "America/New_York" }
},
"nycOlsonMinute": {
$minute: { date: "$date", timezone: "America/New_York" }
}
}
}])

이 연산은 다음과 같은 결과를 반환합니다.

{
"_id": 1,
"nycHour" : 5,
"nycMinute" : 24,
"gmtHour" : 10,
"gmtMinute" : 24,
"nycOlsonHour" : 6,
"nycOlsonMinute" : 24
}

The following aggregation uses $dateFromParts to construct three date objects from the provided input fields:

db.sales.aggregate([
{
$project: {
date: {
$dateFromParts: {
'year' : 2017, 'month' : 2, 'day': 8, 'hour' : 12
}
},
date_iso: {
$dateFromParts: {
'isoWeekYear' : 2017, 'isoWeek' : 6, 'isoDayOfWeek' : 3, 'hour' : 12
}
},
date_timezone: {
$dateFromParts: {
'year' : 2016, 'month' : 12, 'day' : 31, 'hour' : 23,
'minute' : 46, 'second' : 12, 'timezone' : 'America/New_York'
}
}
}
}])

이 연산은 다음과 같은 결과를 반환합니다.

{
"_id" : 1,
"date" : ISODate("2017-02-08T12:00:00Z"),
"date_iso" : ISODate("2017-02-08T12:00:00Z"),
"date_timezone" : ISODate("2017-01-01T04:46:12Z")
}

이 페이지의 내용