정의
버전 5.0의 신규 항목입니다.
날짜를 자릅니다.
$dateTrunc 구문:
{ $dateTrunc: { date: <Expression>, unit: <Expression>, binSize: <Expression>, timezone: <tzExpression>, startOfWeek: <Expression> } }
필드 | 필수/선택 사항 | 설명 |
|---|---|---|
필수 사항 | The date to truncate, specified in UTC. The date can be any expression that resolves to a Date, a Timestamp, or an ObjectID. | |
필수 사항 | 시간 단위로, 다음 문자열 중 하나로 해석되어야 하는 표현식으로 지정됩니다.
Together, binSize and unit specify the time period used in the | |
옵션 | 숫자로 된 시간 값으로, 0이 아닌 양수로 해석되어야 하는 표현식으로 지정됩니다. 기본값은 1입니다. Together, binSize and unit specify the time period used in the | |
옵션 | The timezone for the
If no timezone is provided, the | |
옵션 | The start of the week. Used when unit is startOfWeek는 대소문자를 구분하지 않는 다음과 같은 문자열 중 하나로 리졸브되어야 하는 표현식입니다.
|
행동
다음과 같은 경우
null을 반환합니다.any of the input fields except startOfWeek is missing or set to
null, orif unit is
weekand startOfWeek is missing or set tonull.
1583년 이전 날짜에 대해 1583년 이전 그레고리안 달력을 사용합니다.
일광 절약 시간제를 고려하지만 윤초는 고려하지 않습니다.
binSize 및 unit 필드
Together, binSize and unit specify the time period used in the $dateTrunc calculation.
예를 들면 다음과 같습니다.
If binSize is
1and unit ishour, the time period is one hour. For the date2021-03-20T11:30:05Z,$dateTruncreturns2021-03-20T11:00:00Z.If binSize is
2and unit ishour, the time period is two hours. For the date2021-03-20T11:30:05Z,$dateTruncreturns2021-03-20T10:00:00Z.
Divides the time for the
$dateTrunccalculation into binSize time periods in the specified time unit.The time periods start at a reference date, which is determined by unit. If unit is:
A string other than
week,$dateTruncuses a reference date of2000-01-01T00:00:00.00Z. For example, if binSize is10and unit isyear, example time periods are:2000-01-01T00:00:00.00Z2010-01-01T00:00:00.00Z2020-01-01T00:00:00.00Z
Equal to
week,$dateTruncuses a reference date that is set to the earliest first day of the week that is greater than or equal to2000-01-01. The first day is set using startOfWeek (the default is Sunday).
단위 가 다음과 같은 경우:
year:$dateTruncreturns the ISODate for the start of January 1 for the year in date.quarter:$dateTruncreturns the ISODate for the start of the first day of the calendar quarter in date.분기는 다음과 같습니다.
1월~3월
4월~6월
7월~9월
10월~12월
month:$dateTruncreturns the ISODate for the start of the first day of the month in date.week:$dateTruncreturns the ISODate for the start of the startOfWeek day in date. The default for startOfWeek is Sunday.day:$dateTruncreturns the ISODate for the start of the day in date.hour:$dateTruncreturns the ISODate for the start of the hour in date.minute:$dateTruncreturns the ISODate for the start of the minute in date.second:$dateTruncreturns the ISODate for start of the second in date.
unit 및 startOfWeek 필드
단위 가 다음과 같은 경우:
week이외의 문자열인startOfWeek는 무시됩니다.week과 동일하며 startOfWeek는 다음과 같습니다.Specified:
$dateTruncuses startOfWeek as the first day of the week for the calculation.Omitted:
$dateTruncuses Sunday as the start of the week for the calculation.
예시
캘리포니아주(CA)와 워싱턴주(WA)의 케이크 판매량이 포함된 cakeSales collection을 생성합니다.
db.cakeSales.insertMany( [ { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"), state: "CA", price: 13, quantity: 120 }, { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"), state: "WA", price: 14, quantity: 140 }, { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"), state: "CA", price: 12, quantity: 145 }, { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"), state: "WA", price: 13, quantity: 104 }, { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"), state: "CA", price: 41, quantity: 162 }, { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"), state: "WA", price: 43, quantity: 134 } ] )
다음 예시에서는 cakeSales 컬렉션이 사용됩니다.
$project 파이프라인 단계에서 주문 날짜 잘라내기
This example uses $dateTrunc in a $project stage to truncate the cake sales orderDate values to two weeks:
db.cakeSales.aggregate( [ { $project: { _id: 1, orderDate: 1, truncatedOrderDate: { $dateTrunc: { date: "$orderDate", unit: "week", binSize: 2, timezone: "America/Los_Angeles", startOfWeek: "Monday" } } } } ] )
예시:
$project출력에_id,orderDate,truncatedOrderDate필드가 포함됩니다.$dateTrunctruncates theorderDatefield to a2binSizeweekunit time period in theAmerica/Los_Angelestimezone with startOfWeek set toMonday.
이 예시 출력에서는 잘린 orderDate가 truncatedOrderDate 필드에 표시됩니다.
[ { _id: 0, orderDate: ISODate("2020-05-18T14:10:30.000Z"), truncatedOrderDate: ISODate("2020-05-11T07:00:00.000Z") }, { _id: 1, orderDate: ISODate("2021-03-20T11:30:05.000Z"), truncatedOrderDate: ISODate("2021-03-15T07:00:00.000Z") }, { _id: 2, orderDate: ISODate("2021-01-11T06:31:15.000Z"), truncatedOrderDate: ISODate("2021-01-04T08:00:00.000Z") }, { _id: 3, orderDate: ISODate("2020-02-08T13:13:23.000Z"), truncatedOrderDate: ISODate("2020-02-03T08:00:00.000Z") }, { _id: 4, orderDate: ISODate("2019-05-18T16:09:01.000Z"), truncatedOrderDate: ISODate("2019-05-13T07:00:00.000Z") }, { _id: 5, orderDate: ISODate("2019-01-08T06:12:03.000Z"), truncatedOrderDate: ISODate("2019-01-07T08:00:00.000Z") } ]
$group 파이프라인 단계에서 주문 날짜 잘라내기 및 수량 합계 가져오기
이 예시에서는 $dateTrunc $group orderDate quantity 단계에서를 사용하여 케이크 판매 값을 6개월로 자르고 값의 합계를 반환합니다.
db.cakeSales.aggregate( [ { $group: { _id: { truncatedOrderDate: { $dateTrunc: { date: "$orderDate", unit: "month", binSize: 6 } } }, sumQuantity: { $sum: "$quantity" } } } ] )
예시:
$group은(는)_id필드를truncatedOrderDate필드로 설정하여cakeSales문서를 그룹화하고$sum을 사용하여 각 그룹에 대한quantity값의 합계를 반환합니다.
이 예제의 출력에서는 잘린 orderDate이 truncatedOrderDate 필드에 표시되고 quantity 합계가 sumQuantity 필드에 표시됩니다.
[ { _id: { truncatedOrderDate: ISODate("2020-01-01T00:00:00.000Z") }, sumQuantity: 224 }, { _id: { truncatedOrderDate: ISODate("2021-01-01T00:00:00.000Z") }, sumQuantity: 285 }, { _id: { truncatedOrderDate: ISODate("2019-01-01T00:00:00.000Z") }, sumQuantity: 296 } ]