$dateTrunc
New from version 8.0
Not supported by Elastic cluster.
The $dateTrunc aggregation operator in Amazon DocumentDB truncates a date to a specified unit.
Parameters
-
date: A date expression that resolves to a date or timestamp. -
unit: A string that specifies the time unit for the subtrahend expression. Supported units areyear,quarter,month,week,day,hour,minute,second, andmillisecond.
Example (MongoDB Shell)
The following example demonstrates how to use the $dateTrunc operator to truncate a date to to the hour.
Create sample documents
db.events.insertMany([ { eventName: "Event 1", eventTime: ISODate("2025-04-01T12:15:00Z") }, { eventName: "Event 2", eventTime: ISODate("2025-08-15T14:33:22Z") }, ]);
Query example
db.events.aggregate([ { $project: { eventName: 1, eventTime: 1, truncatedToHour: { $dateTrunc: { date: "$eventTime", unit: "hour" } } } } ]);
Output
[
{
_id: ObjectId('6924a258d66dcae121d29515'),
eventName: 'Event 1',
eventTime: ISODate('2025-04-01T12:15:00.000Z'),
truncatedToHour: ISODate('2025-04-01T12:00:00.000Z')
},
{
_id: ObjectId('6924a258d66dcae121d29516'),
eventName: 'Event 2',
eventTime: ISODate('2025-08-15T14:33:22.000Z'),
truncatedToHour: ISODate('2025-08-15T14:00:00.000Z')
}
]
Code examples
To view a code example for using the $dateTrunc command, choose the tab for the language that you want to use: