$dateTrunc (aggregation)
On this page本页内容
Definition定义
$dateTrunc
New in version 5.0. 5.0版新增。
Truncates a date.截断日期。
$dateTrunc
syntax:语法:
{
$dateTrunc: {
date: <Expression>,
unit: <Expression>,
binSize: <Expression>,
timezone: <tzExpression>,
startOfWeek: <Expression>
}
}
date | date 、Timestamp 或ObjectID 的任何表达式。 | |||||||
unit |
$dateTrunc calculation. binSize 和unit 一起指定$dateTrunc 计算中使用的时间段。 | |||||||
binSize | $dateTrunc calculation. binSize 和unit 一起指定$dateTrunc 计算中使用的时间段。 | |||||||
timezone | $dateTrunc calculation, specified as an expression that must resolve to a string that contains one of these values: $dateTrunc 计算的时区,指定为必须解析为包含以下值之一的字符串的表达式:
$dateTrunc calculation is performed in UTC. $dateTrunc 计算将以UTC执行。
| |||||||
startOfWeek | week . Defaults to Sunday .week 时使用。默认为Sunday 。startOfWeek 是一个表达式,必须解析为以下不区分大小写的字符串之一:
|
See also: 另请参阅:
Behavior行为
Returns如果出现以下情况,则返回null
if:null
:any of the input fields except startOfWeek is missing or set to除null
, orstartOfWeek
之外的任何输入字段丢失或设置为null
,或者if unit is如果week
and startOfWeek is missing or set tonull
.unit
为week
并且startOfWeek
丢失或设置为null
。
Uses the proleptic Gregorian calendar1583年之前的日期使用令人怀疑的格里高利历for dates preceding the year 1583.
。
Accounts for Daylight Savings Time, but does not account for leap seconds.计入夏令时,但不计入闰秒。
binSize
and unit
FieldsbinSize
和unit
字段
binSize
and unit
FieldsTogether, binSize and unit specify the time period used in the $dateTrunc
calculation.binSize
和unit
一起指定$dateTrunc
计算中使用的时间段。
For example:例如:
If binSize is如果1
and unit ishour
, the time period is one hour.binSize
为1
,unit
为hour
,则时间段为一小时。For the date对于2021-03-20T11:30:05Z
,$dateTrunc
returns2021-03-20T11:00:00Z
.date
2021-03-20T111:30:05Z
,$dateTrunc
返回2021-03-20T11:00:00Z
。If binSize is如果2
and unit ishour
, the time period is two hours.binSize
为2
,unit
为hour
,则时间段为两小时。For the date对于2021-03-20T11:30:05Z
,$dateTrunc
returns2021-03-20T10:00:00Z
.date
2021-03-20T11:30:05Z
,$dateTrunc
返回2021-03-20T10:00:00Z
。
Divides the time for the将$dateTrunc
calculation into binSize time periods in the specified time unit.$dateTrunc
计算的时间划分为指定时间unit
的binSize
时间段。The time periods start at a reference date, which is determined by unit.时间段从参考日期开始,该日期由unit
确定。If unit is:如果unit
为:A string other than是week
,$dateTrunc
uses a reference date of2000-01-01T00:00:00.00Z
.week
以外的字符串,则$dateTrunc
使用的参考日期为2000-01-01T00:00:00.00Z
。For example, if binSize is例如,如果10
and unit isyear
, example time periods are:binSize
为10
,unit
为year
,则时间段示例为:2000-01-01T00:00:00.00Z
2010-01-01T00:00:00.00Z
2020-01-01T00:00:00.00Z
Equal to等于week
,$dateTrunc
uses a reference date that is set to the earliest first day of the week that is greater than or equal to2000-01-01
.week
,则$dateTrunc
使用的参考日期设置为一周中最早的第一天,大于或等于2000-01-01
。The first day is set using startOfWeek (the default is Sunday).第一天是使用startOfWeek
设置的(默认为周日)。
Returns the lower boundary of the time period that the date is in.返回date
所在时间段的下限。The boundary is returned as an ISODate.边界将作为ISODate返回。If the binSize field is如果1
,$dateTrunc
sets the least significant parts (as determined by unit) of the returned ISODate to0
and keeps the rest of the ISODate the same.binSize
字段为1
,$dateTrunc
将返回的ISODate的最低有效部分(由unit
确定)设置为0
,并保持ISODate
的其余部分不变。
year
::$dateTrunc
returns the ISODate for the start of January 1 for the year in date.$dateTrunc
返回date
中年份的1月1日开始的ISODate。quarter
::$dateTrunc
returns the ISODate for the start of the first day of the calendar quarter in date.$dateTrunc
返回日期中日历季度第一天开始的ISODate
。The quarters are:季度为:January to March1月至3月April to June四月至六月July to September七月至九月October to December十月至十二月
month
::$dateTrunc
returns the ISODate for the start of the first day of the month in date.$dateTrunc
返回date
中月份第一天开始的ISODate。week
::$dateTrunc
returns the ISODate for the start of the startOfWeek day in date.$dateTrunc
返回日期中startOfWeek
日开始的ISODate。The default for startOfWeek is Sunday.startOfWeek
的默认值为周日。day
::$dateTrunc
returns the ISODate for the start of the day in date.$dateTrunc
返回date
中一天开始的ISODate。hour
::$dateTrunc
returns the ISODate for the start of the hour in date.$dateTrunc
返回date
中小时开始的ISODate。minute
::$dateTrunc
returns the ISODate for the start of the minute in date.$dateTrunc
返回date
中分钟开始的ISODate。second
::$dateTrunc
returns the ISODate for start of the second in date.$dateTrunc
返回date
中秒开始的ISODate。
unit
and startOfWeek
Fieldsunit
和startOfWeek
字段
unit
and startOfWeek
FieldsA string other than忽略week
, startOfWeek is ignored.week
以外的字符串startOfWeek
。Equal to等于week
and startOfWeek is:week
,startOfWeek
为:Specified:指定:$dateTrunc
uses startOfWeek as the first day of the week for the calculation.$dateTrunc
使用startOfWeek
作为一周中的第一天进行计算。Omitted:省略:$dateTrunc
uses Sunday as the start of the week for the calculation.$dateTrunc
使用星期日作为一周的开始进行计算。
Examples实例
Create a 创建一个包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
系列:
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 }
] )
The cakeSales
collection is used in the following examples.cakeSales
集合用于以下示例。
Truncate Order Dates in a $project
Pipeline Stage截断$project
管道阶段中的订单日期
$project
Pipeline StageThis example uses 本例在$dateTrunc
in a $project
stage to truncate the cake sales orderDate
values to two weeks:$project
阶段使用$dateTrunc
将蛋糕销售orderDate
值截断为两周:
db.cakeSales.aggregate( [
{
$project: {
_id: 1,
orderDate: 1,
truncatedOrderDate: {
$dateTrunc: {
date: "$orderDate", unit: "week", binSize: 2,
timezone: "America/Los_Angeles", startOfWeek: "Monday"
}
}
}
}
] )
In the example:在示例中:
$project
includes the在输出中包括_id
,orderDate
, andtruncatedOrderDate
fields in the output._id
、orderDate
和truncatedOrderDate
字段。$dateTrunc
truncates the将orderDate
field to a2
binSizeweek
unit time period in theAmerica/Los_Angeles
timezone with startOfWeek set toMonday
.orderDate
字段截断为America/Los_Angeles
时区中的2
binSize
week
unit
时间段,并且startOfWeek
设置为Monday
。
In this example output, the truncated 在本示例输出中,截断的orderDate
is shown in the truncatedOrderDate
field: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")
}
]
Truncate Order Dates and Obtain Quantity Sum in a $group
Pipeline Stage截断订单日期并获得$group
管道阶段中的数量总和
$group
Pipeline StageThis 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:$group
阶段使用$dateTrunc
将蛋糕销售orderDate
值截断为六个月,并返回quantity
值的总和:
db.cakeSales.aggregate( [
{
$group: {
_id: {
truncatedOrderDate: {
$dateTrunc: {
date: "$orderDate", unit: "month", binSize: 6
}
}
},
sumQuantity: { $sum: "$quantity" }
}
}
] )
In the example:在示例中:
$group
has the_id
field set to thetruncatedOrderDate
field to group thecakeSales
documents, and returns the sum of thequantity
values for each group using$sum
.$group
将_id
字段设置为truncatedOrderDate
字段,以对cakeSales
文档进行分组,并使用$sum
返回每组quantity
值的总和。$dateTrunc
truncates theorderDate
field to a6
binSizemonth
unit time period.$dateTrunc
将orderDate
字段截断为6
binSize
month
unit
时间段。
In this example output, the truncated 在此示例输出中,截断的orderDate
is shown in the truncatedOrderDate
field and the quantity
sum is shown in the sumQuantity
field: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
}
]