On this page本页内容
New in version 5.0.在5.0版中新增。
Truncates a date.截断日期。
$dateTrunc syntax:语法:
{
$dateTrunc: {
date: <Expression>,
unit: <Expression>,
binSize: <Expression>,
timezone: <tzExpression>,
startOfWeek: <Expression>
}
}
| date |
| |||||||
| unit |
| |||||||
| binSize |
| |||||||
| timezone |
| |||||||
| startOfWeek |
|
Returns 如果出现以下情况,则返回null if:null:
null, orstartOfWeek之外的任何输入字段缺失或设置为null,或week and startOfWeek is missing or set to null.unit为week,且startOfWeek缺失或设置为null。binSizeunitTogether, binSize and unit specify the time period used in the $dateTrunc calculation.binSize和unit一起指定$dateTrunc计算中使用的时间段。
For example:例如:
1 and unit is hours, the time period is one hour. binSize为1,unit为hours,则时间段为一小时。2021-03-20T11:30:05Z, $dateTrunc returns 2021-03-20T11:00:00Z.2021-03-20T11:30:05Z,$dateTrunc返回2021-03-20T11:00:00Z。2 and unit is hours, the time period is two hours. binSize为2,unit为hours,则时间段为两小时。2021-03-20T11:30:05Z, $dateTrunc returns 2021-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 determind by unit. 时间段从参考日期开始,参考日期由unit决定。If unit is:如果unit为:
A string other than 除了week, $dateTrunc uses a reference date of 2000-01-01T00:00:00.00Z. week,$dateTrunc之外的字符串使用的参考日期为2000-01-01T00:00:00.00Z。For example, if binSize is 例如,如果10 and unit is year, example time periods are:binSize为10,unit为year,则时间段示例为:
2000-01-01T00:00:00.00Z2010-01-01T00:00:00.00Z2020-01-01T00:00:00.00Zweek, $dateTrunc uses a reference date that is set to the earliest first day of the week that is greater than or equal to 2000-01-01. week,$dateTrunc使用的参考日期设置为一周中最早的第一天,大于或等于2000-01-01。startOfWeek设置第一天(默认值为星期日)。date所在时间段的下限。1, $dateTrunc sets the least significant parts (as determined by unit) of the returned ISODate to 0 and keeps the rest of the ISODate the same.binSize字段为1,$dateTrunc将返回的ISODate设置为0的最低有效部分(由unit确定),并保持ISODate的其余部分不变。If unit is:如果unit为:
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返回日历季度date第一天开始的ISODate。
The quarters are:季度为:
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返回date中startOfWeek日开始的ISODate。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 the ISODate for start of the second in date.$dateTrunc返回date中一秒钟开始的ISODate。unitstartOfWeekIf unit is:如果unit为:
week, startOfWeek is ignored.week以外的字符串,则忽略startOfWeek。Equal to 等于week and startOfWeek is:week并且startOfWeek是:
$dateTrunc uses startOfWeek as the first day of the week for the calculation.$dateTrunc使用startOfWeek作为计算周的第一天。$dateTrunc uses Sunday as the start of the week for the calculation.$dateTrunc使用星期天作为计算周的开始。Create a 创建cakeSales collection that contains cake sales in the states of California (CA) and Washington (WA):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 }
] )
The 以下示例中使用了cakeSales collection is used in the following examples.cakeSales集合。
$project Pipeline Stage$project管道阶段中的订单日期This 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, and truncatedOrderDate fields in the output.$project在输出中包括_id、orderDate和truncatedOrderDate字段。$dateTrunc truncates the orderDate field to a 2binSize week unit time period in the America/Los_Angelestimezone with startOfWeek set to Monday.$dateTrunc将orderDate字段截断为America/Los_Angeles时区中的2 binSize周单位时间段,startOfWeek设置为星期一。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")
}
]
$group Pipeline Stage$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:$group阶段使用$dateTrunc将蛋糕销售订单日期值截断为六个月,并返回数量值之和:
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 the truncatedOrderDate field to group the cakeSales documents, and returns the sum of the quantity values for each group using $sum.$group将_id字段设置为truncatedOrderDate字段,以对cakeSales文档进行分组,并使用$sum返回每个组的quantity值之和。$dateTrunc truncates the orderDate field to a 6binSize month unit time period.$dateTrunc将orderDate字段截断为6个月的单位时间段。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
}
]