The $dateTrunc operator rounds a BSON Date down to the start of a time period — such as midnight for a day, the first of the month, or the top of the hour. Use it for time-series charts, daily rollups, and grouping events into calendar buckets.
01
Truncate Dates
Date → period start.
02
Syntax
date, unit.
03
Time Units
day, month, hour…
04
binSize
Wider time bins.
05
Use Cases
Charts, rollups.
06
vs $dateToParts
Boundary vs parts.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateTrunc operator takes a timestamp and returns the BSON Date at the beginning of the chosen unit period. For example, truncating ISODate("2024-06-15T14:30:45Z") to unit: "day" yields ISODate("2024-06-15T00:00:00Z") — the start of that calendar day in UTC.
💡
Beginner Tip
$dateTrunc returns a Date, not a string or number. It always rounds down to the period boundary. Requires MongoDB 5.0+.
Foundation
📝 Syntax
The $dateTrunc operator takes an object with a required date and unit:
Using a truncated date as the $group key is cleaner than string formatting. All orders in June 2024 share one bucket starting at the first instant of that month.
Example 3 — Two-Hour Bins with binSize
Use binSize: 2 with unit: "hour" for wider time-series buckets:
mongosh
db.metrics.aggregate([
{
$project: {
metric: 1,
recordedAt: 1,
twoHourBin: {
$dateTrunc: {
date: "$recordedAt",
unit: "hour",
binSize: 2
}
}
}
}
])
// 09:30 and 10:45 → same bin (08:00–10:00 boundary)
// 11:15 → next bin (10:00–12:00 boundary)
How It Works
binSize groups multiple units into one bin. Useful for dashboards that do not need minute-level precision but still want consistent Date keys instead of strings.
Example 4 — Truncate to Local Calendar Day
Apply a timezone so daily buckets follow local midnight, not UTC:
Without timezone, unit: "day" uses UTC midnights. With an Olson ID, truncation respects local calendar days — important for regional analytics and daylight saving boundaries.
Applications
🚀 Use Cases
Time-series charts — bucket events by hour, day, or month for graph axes.
Revenue rollups — aggregate sales by truncated month or quarter boundaries.
Log analysis — count errors per day without manual date math.
Retention cohorts — assign users to signup-week or signup-month buckets.
🧠 How $dateTrunc Works
1
MongoDB resolves the input date
date is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dateTrunc finds the period boundary
MongoDB rounds down to the start of the unit (and binSize), optionally using timezone.
Truncate
3
A boundary Date is returned
The truncated timestamp is ready as a $group key or chart bucket label source.
Output
=
📅
Clean time buckets
Pair with $group and $dateToString for labels, or $dateDiff to measure spans between buckets.
Wrap Up
Conclusion
The $dateTrunc operator is the clearest way to bucket BSON dates by calendar periods in MongoDB aggregation pipelines. It replaces manual midnight calculations and string-based grouping with a single, readable expression that returns proper Date values.
Use timezone when buckets must follow local days, and binSize when you need wider intervals. Pair with $dateToString for display labels and ensure your MongoDB deployment is version 5.0 or later.
Use $dateTrunc as $group keys for time-series rollups
Set timezone for local calendar bucketing
Pick the smallest unit that matches your chart granularity
Use binSize for coarser buckets without changing unit logic
Keep original timestamps alongside truncated buckets for drill-down
❌ Don’t
Use $dateTrunc on MongoDB versions before 5.0
Confuse truncation with rounding up — it always floors to the boundary
Assume UTC day buckets match local business days without timezone
Store truncated dates as strings — keep BSON Date type for sorting
Duplicate bucketing logic in app code when $dateTrunc suffices
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateTrunc
Use these points when bucketing dates by time periods in MongoDB.
5
Core concepts
📅01
Truncate Dates
Date → period start.
Purpose
📝02
Two Required
date, unit.
Syntax
🛠03
Date Output
Boundary timestamp.
Output
🌐04
binSize
Wider time bins.
Option
⚠05
MongoDB 5.0+
Version requirement.
Important
❓ Frequently Asked Questions
$dateTrunc rounds a BSON Date down to the start of a time period — such as the beginning of the day, hour, or month. Use it for time-series bucketing, daily rollups, and grouping events by calendar periods.
{ $dateTrunc: { date: <date>, unit: <string>, binSize: <number>, timezone: <string>, startOfWeek: <string> } }. date and unit are required. binSize defaults to 1.
year, quarter, month, week, day, hour, minute, second, and millisecond. Pass the unit as a string such as "day" or "month".
$dateTrunc returns a BSON Date at the start of a period boundary. $dateToParts returns an object of numeric components like year and month. Use $dateTrunc when you need a date for grouping; use $dateToParts when you need separate integer fields.
$dateTrunc is available in MongoDB 5.0 and later in aggregation pipelines and update pipelines that use aggregation expressions.