$dateTrunc (Truncate Date)
Last updated: 2026-10-09
$dateTrunc (Truncate Date)
$dateTrunc truncates a date to the start of a given unit and returns the new date — perfect for aligning timestamps into hour/day/month buckets for stats. unit supports year, quarter, month, week, day, hour, minute, and second; binSize widens the bucket (e.g. 3-hour buckets). startOfWeek applies only to week. timezone drives the calendar used for truncation — set it explicitly across zones. Introduced in MongoDB 5.0.
| Category | Date |
|---|---|
| MongoDB Version | 5.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $dateTrunc: { date: <date-expression>, unit: <unit>, binSize: <number>, timezone: <timezone>, startOfWeek: <week-start> } }
⚙️ Parameters
| date | Required; the date to truncate. |
|---|---|
| unit | Required; truncation unit: year, quarter, month, week, day, hour, minute, second. |
| binSize | Optional bucket size that widens the unit. |
| timezone | Optional timezone for truncation. |
| startOfWeek | Optional; applies only when unit is week. |
Returns:The truncated date (BSON Date); null if date is null or missing.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $dateTrunc: aggregate sales by hour
db.sales.aggregate([
{ $group: {
_id: { $dateTrunc: { date: '$soldAt', unit: 'hour' } },
total: { $sum: '$amount' }
} }
])
Output:
{ "_id": ISODate("2026-09-25T08:00:00Z"), "total": 1250 }
{ "_id": ISODate("2026-09-25T09:00:00Z"), "total": 980 }
📌 Notes
- Result is always a unit start: day gives 00:00, hour gives the top of the hour
- binSize supports minute, hour, day, week, month, quarter, year
- quarter splits the year into 4; result is the first day of the quarter
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow is this different from $dateAdd?
A$dateTrunc only aligns to a unit start; $dateAdd moves the time.
QWhat does binSize mean?
AIt widens the unit into buckets — binSize 3 with unit hour gives 3-hour buckets.
QWhat time is the result?
Aday gives 00:00 of that day; hour gives the top of the hour.