AI 에이전트의 경우: 문서 인덱스는 https://www.mongodb.com/ko-kr/docs/llms.txt에서 사용할 수 있으며, 모든 페이지의 마크다운 버전은 어떤 URL 경로에 .md를 추가하여 사용할 수 있습니다.
Docs Menu

$dateFromString (표현식 연산자)

$dateFromString

날짜/시간 문자열을 날짜 객체로 변환합니다.

The $dateFromString expression has the following syntax:

{ $dateFromString: {
dateString: <dateStringExpression>,
format: <formatStringExpression>,
timezone: <tzExpression>,
onError: <onErrorExpression>,
onNull: <onNullExpression>
} }

The $dateFromString takes a document with the following fields:

필드
설명

dateString

날짜 객체로 변환할 날짜/시간 문자열입니다. 날짜/시간 형식에 대한 자세한 내용은 Date()를 참조하세요.

연산자에 timezone 옵션을 지정하는 경우 dateString에 구역 정보를 포함하지 마세요.

format

Optional. The date format specification of the dateString. The format can be any expression that evaluates to a string literal, containing 0 or more format specifiers. For a list of specifiers available, see Format Specifiers.

If unspecified, $dateFromString uses "%Y-%m-%dT%H:%M:%S.%LZ" as the default format but accepts a variety of formats and attempts to parse the dateString if possible.

timezone

선택 사항입니다. 날짜 서식을 지정하는 데 사용할 표준 시간대입니다.

dateString 인수가 '2017-02-08T12:10:40.787Z'와 같은 형식이고 끝의 'Z'가 Zulu 시간(UTC 표준 시간대)을 나타내는 경우 timezone 인수를 지정할 수 없습니다.

<timezone> 은 다음과 같은 옵션과 이를 평가하는 표현식을 사용할 수 있습니다.

  • Olson 표준 시간대 식별자(예: "Europe/London" 또는 "America/New_York") 또는

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

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

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

    • +/-[hh], 예: "+03", 또는

  • 문자열 "Z", "UTC" 또는 "GMT"

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

onError

Optional. If $dateFromString encounters an error while parsing the given dateString, it outputs the result value of the provided onError expression. This result value can be of any type.

If you do not specify onError, $dateFromString throws an error if it cannot parse dateString.

onNull

Optional. If the dateString provided to $dateFromString is null or missing, it outputs the result value of the provided onNull expression. This result value can be of any type.

If you do not specify onNull and dateString is null or missing, then $dateFromString outputs null.

예시
결과
{ $dateFromString: {
dateString: "2017-02-08T12:10:40.787"
} }

ISODate("2017-02-08T12:10:40.787Z")

{ $dateFromString: {
dateString: "2017-02-08T12:10:40.787",
timezone: "America/New_York"
} }

ISODate("2017-02-08T17:10:40.787Z")

{ $dateFromString: {
dateString: "2017-02-08"
} }

ISODate("2017-02-08T00:00:00Z")

{ $dateFromString: {
dateString: "oct 20 2020"
} }

ISODate("2020-10-20T00:00:00.000Z")

{ $dateFromString: {
dateString: "06-15-2018",
format: "%m-%d-%Y"
} }

ISODate("2018-06-15T00:00:00Z")

{ $dateFromString: {
dateString: "15-06-2018",
format: "%d-%m-%Y"
} }

ISODate("2018-06-15T00:00:00Z")

{ $dateFromString: {
dateString: "WED jan 31 12:05:28 +03:30 1996"
} }

ISODate("1996-01-31T08:35:28.000Z")

<formatString>에서 사용할 수 있는 형식 지정자:

지정자
설명
Possible Values

%b

월 이름 축약형(3자)

jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec

%B

월 이름 전체

january-december

%d

월과 요일(2자리, 제로 패딩)

01-31

%G

ISO 8601 형식의 연도

0000-9999

%H

시(2자리, 제로 패딩, 24시간 시계)

00-23

%j

1년 기준 일수(3자리, 영숫자)

001-366

%L

밀리초(3자리, 제로 패딩)

000-999

%m

월(2자리, 제로 패딩)

01-12

%M

분(2자리, 제로 패딩)

00-59

%S

초(2자리, 제로 패딩)

00-60

%u

ISO 8601 형식의 요일 번호(1-월요일, 7-일요일)

1-7

%U

연도별 주(2자리, 제로 패딩)

00-53

%V

ISO 8601 형식의 연도별 주

1-53

%w

정수로 표현된 요일(0-일요일, 6-토요일)

0-6

%Y

연도(4자리, 0 패딩)

0000-9999

%z

UTC의 표준 시간대 오프셋.

+/-[hh][mm]

%Z

UTC에서 오프셋된 분을 숫자로 표시합니다. 예를 들어 표준 시간대 오프셋(+/-[hhmm])이 +0445이면 분 오프셋은 +285입니다.

+/-mmm

%%

리터럴로서의 백분율 문자

%

날짜가 있는 다음 문서가 포함된 collection logmessages를 고려해 보겠습니다.

{ _id: 1, date: "2017-02-08T12:10:40.787", timezone: "America/New_York", message: "Step 1: Started" },
{ _id: 2, date: "2017-02-08", timezone: "-05:00", message: "Step 1: Ended" },
{ _id: 3, message: " Step 1: Ended " },
{ _id: 4, date: "2017-02-09", timezone: "Europe/London", message: "Step 2: Started"},
{ _id: 5, date: "2017-02-09T03:35:02.055", timezone: "+0530", message: "Step 2: In Progress"}

다음 집계는 $dateFromString을 사용하여 date 값을 날짜 객체로 변환합니다.

db.logmessages.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: 'America/New_York'
}
}
}
} ] )

위 집계는 다음 문서를 반환하고 각 date 필드를 동부 표준 시간대로 변환합니다.

{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
{ "_id" : 3, "date" : null }
{ "_id" : 4, "date" : ISODate("2017-02-09T05:00:00Z") }
{ "_id" : 5, "date" : ISODate("2017-02-09T08:35:02.055Z") }

하드 코딩된 인수 대신 문서 필드를 통해 timezone 인수를 제공할 수도 있습니다. 예시:

db.logmessages.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone'
}
}
}
} ] )

위 집계는 다음 문서를 반환하고 각 date 필드를 해당 UTC 표현으로 변환합니다.

{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("2017-02-08T05:00:00Z") }
{ "_id" : 3, "date" : null }
{ "_id" : 4, "date" : ISODate("2017-02-09T00:00:00Z") }
{ "_id" : 5, "date" : ISODate("2017-02-08T22:05:02.055Z") }

If your collection contains documents with unparsable date strings, $dateFromString throws an error unless you provide an aggregation expression to the optional onError parameter.

예를 들어, 컬렉션 dates에 다음 문서가 포함되어 있다고 가정해 보겠습니다.

{ "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
{ "_id" : 2, "date" : "20177-02-09T03:35:02.055", timezone: "America/New_York" }

onError 매개변수를 사용하여 잘못된 날짜를 원래 문자열 형식으로 반환할 수 있습니다.

db.dates.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone',
onError: '$date'
}
}
}
} ] )

이 경우 다음과 같은 문서가 반환됩니다.

{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : "20177-02-09T03:35:02.055" }

If your collection contains documents with null date strings, $dateFromString returns null unless you provide an aggregation expression to the optional onNull parameter.

예를 들어, 컬렉션 dates에 다음 문서가 포함되어 있다고 가정해 보겠습니다.

{ "_id" : 1, "date" : "2017-02-08T12:10:40.787", timezone: "America/New_York" },
{ "_id" : 2, "date" : null, timezone: "America/New_York" }

You can use the onNull parameter to have $dateFromString return a date representing the unix epoch instead of null:

db.dates.aggregate( [ {
$project: {
date: {
$dateFromString: {
dateString: '$date',
timezone: '$timezone',
onNull: new Date(0)
}
}
}
} ] )

이 경우 다음과 같은 문서가 반환됩니다.

{ "_id" : 1, "date" : ISODate("2017-02-08T17:10:40.787Z") }
{ "_id" : 2, "date" : ISODate("1970-01-01T00:00:00Z") }
이 페이지 평가하기

이 페이지의 내용