- Reference >
- Operators >
- Aggregation Pipeline Operators >
- $dateTrunc (aggregation)
$dateTrunc (aggregation)¶
On this page
Definition¶
-
$dateTrunc¶
New in version 5.0.
Truncates a date.
$dateTrunc syntax:
| Field | Required / Optional | Description | ||||||
|---|---|---|---|---|---|---|---|---|
| date | Required | The date to truncate, specified in UTC. The date can be any expression that resolves to a Date, a Timestamp, or an ObjectID. |
||||||
| unit | Required | The unit of time, specified as an expression that must resolve to one of these strings:
Together, binSize and unit specify the time period used in the
|
||||||
| binSize | Optional | The numeric time value, specified as an expression that must resolve to a positive non-zero number. Defaults to 1. Together, binSize and unit specify the time period used in the
|
||||||
| timezone | Optional | The timezone for the If no timezone is provided, the
|
||||||
| startOfWeek | Optional | The start of the week. Used when unit is
startOfWeek is an expression that must resolve to one of these case insensitive strings:
|
See also
Behavior¶
- Returns
nullif:- any of the input fields except startOfWeek is missing or set to
null, or - if unit is
weekand startOfWeek is missing or set tonull.
- any of the input fields except startOfWeek is missing or set to
- Uses the :wikipedia:`proleptic Gregorian calendar <Proleptic_Gregorian_calendar>` for dates preceding the year 1583.
- Accounts for Daylight Savings Time, but does not account for :wikipedia:`leap seconds <Leap_second>`.
binSize and unit Fields¶
Together, binSize and unit specify the time period used in the
$dateTrunc calculation.
For example:
- If binSize is
1and unit ishours, the time period is one hour. For the date2021-03-20T11:30:05Z,$dateTruncreturns2021-03-20T11:00:00Z. - If binSize is
2and unit ishours, 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 determind 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).
- A string other than
Returns the lower boundary of the time period that the date is in. The boundary is returned as an ISODate. If the binSize field is
1,$dateTruncsets the least significant parts (as determined by unit) of the returned ISODate to0and keeps the rest of the ISODate the same.
If unit is:
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.The quarters are:
- January to March
- April to June
- July to September
- October to December
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 the ISODate for start of the second in date.
unit and startOfWeek Fields¶
If unit is:
- A string other than
week, startOfWeek is ignored. - Equal to
weekand startOfWeek is:- 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.
- Specified:
Examples¶
Create a cakeSales collection that contains cake sales in the states
of California (CA) and Washington (WA):
The cakeSales collection is used in the following examples.
Truncate Order Dates in a $project Pipeline Stage¶
This example uses $dateTrunc in a $project
stage to truncate the cake sales orderDate values to two weeks:
In the example:
$projectincludes the_id,orderDate, andtruncatedOrderDatefields in the output.$dateTrunctruncates theorderDatefield to a2binSizeweekunit time period in theAmerica/Los_Angelestimezone with startOfWeek set toMonday.
In this example output, the truncated orderDate is shown in the
truncatedOrderDate field:
Truncate Order Dates and Obtain Quantity Sum in a $group Pipeline Stage¶
This example uses $dateTrunc in a $group stage
to truncate the cake sales orderDate values to six months and
return the sum of the quantity values:
In the example:
$grouphas the_idfield set to thetruncatedOrderDatefield to group thecakeSalesdocuments, and returns the sum of thequantityvalues for each group using$sum.$dateTrunctruncates theorderDatefield to a6binSizemonthunit time period.
In this example output, the truncated orderDate is shown in the
truncatedOrderDate field and the quantity sum is shown in the
sumQuantity field: