定義
バージョン 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 | |||||||
任意 | 数値の時間値。式として指定され、ゼロ以外の正の数値に解決される必要があります。デフォルトは 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 is an expression that must resolve to one of these case insensitive strings:
|
Tip
動作
次の場合に
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 グレゴリオ暦を使用します。
夏時間は考慮されますが、 うるう秒は考慮されません。
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).
ユニットが次の場合。
year: は$dateTruncdate1 の年の 1 月 からのISODateを返します。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以外の string では、 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 コレクションを作成します。
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 } ]