定义
版本 5.0 中的新增功能。
截断日期。
$dateTrunc 事务语法:
{ $dateTrunc: { date: <Expression>, unit: <Expression>, binSize: <Expression>, timezone: <tzExpression>, startOfWeek: <Expression> } }
字段 | 必需/可选 | 说明 | ||||||
|---|---|---|---|---|---|---|---|---|
必需 | ||||||||
必需 | 时间单位,指定为必须解析为以下字符串之一的表达式:
Together, binSize and unit specify the time period used in the | |||||||
Optional | 数值时间值,指定为必须解析为非零正数的表达式。默认为 1。 Together, binSize and unit specify the time period used in the | |||||||
Optional | The timezone for the If no timezone is provided, the
| |||||||
Optional | 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.
会考虑夏令时,但不会考虑闰秒。
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.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).
如果 unit 为:
year:$dateTrunc在日期中返回当年 1 月开始的ISODate1 。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
如果 unit 为:
除
week之外的字符串,sartOfWeek 将被忽略。等于
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.
示例
创建cakeSales集合,其中包含加利福尼亚州 ( CA ) 和华盛顿州 ( WA ) 的蛋糕销售情况:
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 管道阶段截断订单日期并获取数量总和
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:
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 } ]