The $dateToParts operator decomposes a BSON Date into numeric parts such as year, month, day, hour, minute, second, and millisecond. Use it for dashboards, grouping by calendar month, birthday matching (ignoring year), or feeding values into $dateFromParts.
01
Split Dates
Date → parts object.
02
Syntax
date field required.
03
Components
year, month, day, time.
04
Timezone
Local calendar parts.
05
Use Cases
Reports, grouping.
06
vs $dateFromParts
Split vs build.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateToParts operator takes a single BSON Date and returns a document of integer components. For example, ISODate("2024-06-15T14:30:00Z") becomes { year: 2024, month: 6, day: 15, hour: 14, minute: 30, second: 0, millisecond: 0 } (in UTC unless you specify a timezone).
💡
Beginner Tip
$dateToParts returns an object, not a single number. Access fields with dot notation in later stages, e.g. $dateParts.year. Requires MongoDB 5.0+.
Foundation
📝 Syntax
The $dateToParts operator takes an object with a required date and optional options:
The same UTC instant can fall on different calendar days in different zones. Always set timezone when business reports use local dates rather than server UTC.
Example 3 — Group Sales by Year and Month
Use $dateToParts inside $group to bucket revenue by calendar month:
Grouping on year and month from $dateToParts is cleaner than string formatting for analytics. Pair with $dateFromParts if you need month start/end boundaries.
Example 4 — ISO Week Fields for Reporting
Enable iso8601: true to get ISO week year and week number:
ISO week fields follow the ISO 8601 calendar (week 1 contains the first Thursday of the year). Use them for weekly KPIs that must align with international week numbering.
Applications
🚀 Use Cases
Monthly and yearly reports — group or filter by extracted year and month.
Birthday reminders — match on month and day while ignoring year.
Timezone-aware dashboards — show local calendar parts to regional users.
Data transformation — split dates before writing separate columns or calling $dateFromParts.
🧠 How $dateToParts Works
1
MongoDB resolves the input date
date is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dateToParts decomposes the timestamp
MongoDB applies timezone and optional iso8601, then extracts calendar parts.
Split
3
A parts object is returned
Integer fields like year and month are ready for projection, grouping, or matching.
Output
=
📅
Structured date components
Use in $group, $match, charts, or round-trip with $dateFromParts for custom date logic.
Wrap Up
Conclusion
The $dateToParts operator is the standard way to break BSON dates into numeric components in MongoDB aggregation pipelines. It powers calendar-based grouping, local timezone reporting, and structured date transforms without string parsing.
Pair it with $dateFromParts when you need to rebuild dates from parts, or $dateToString when you need human-readable formatted output. Always specify timezone when reports must reflect local calendar days.
Group on year and month for clean monthly analytics
Remember month is 1–12, matching $dateFromParts
Use iso8601: true when ISO week numbering is required
Store BSON dates at insert time; split at query time when needed
❌ Don’t
Use $dateToParts on MongoDB versions before 5.0
Confuse $dateToParts with $dateFromParts
Assume UTC parts match local business dates without timezone
Expect a single number — the result is always an object
Parse strings with $dateToParts — convert with $dateFromString first
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateToParts
Use these points when splitting dates into components in MongoDB.
5
Core concepts
📅01
Split Dates
Date → parts object.
Purpose
📝02
One Required
date field.
Syntax
🛠03
Object Output
year, month, day…
Output
🌐04
Timezone
Local calendar parts.
Option
⚠05
Not $dateFromParts
Split vs build.
Important
❓ Frequently Asked Questions
$dateToParts breaks a BSON Date into numeric components such as year, month, day, hour, minute, second, and millisecond. Use it for reporting, grouping by calendar parts, or preparing data for $dateFromParts.
{ $dateToParts: { date: <dateExpression>, timezone: <string>, iso8601: <boolean> } }. Only date is required. The operator returns a document of integer parts, not a single number.
$dateToParts splits one BSON Date into components. $dateFromParts assembles components into one BSON Date. They are inverse operations in the date operator family.
When you pass an Olson timezone ID such as "America/New_York", MongoDB extracts parts as they appear in that zone — useful for local calendar reporting instead of raw UTC values.
$dateToParts is available in MongoDB 5.0 and later in aggregation pipelines and update pipelines that use aggregation expressions.