The $month operator extracts the month (1–12) from a BSON Date inside MongoDB aggregation pipelines. Use it for seasonal sales reports, monthly dashboards, quarter filters, and year-over-year month comparisons.
01
Extract Month
Date → 1–12.
02
Syntax
One date argument.
03
1-Based
Jan=1, Dec=12.
04
Timezone Option
Local month support.
05
Use Cases
Sales, seasons.
06
Related Ops
$year, $dayOfMonth.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $month operator takes a date expression and returns an integer from 1 to 12. For example, $month applied to ISODate("2024-06-15T14:30:00Z") returns 6 (June), and ISODate("2024-12-31T23:59:59Z") returns 12 (December).
Unlike JavaScript’s Date.getMonth(), which is 0-based (January = 0), MongoDB’s $month is 1-based. January is always 1 and December is always 12.
💡
Beginner Tip
$month uses UTC by default. For seasonal business rules in local time (e.g., fiscal calendars), use the object form with a timezone option.
Foundation
📝 Syntax
The $month operator takes a date expression, or an object with date and timezone:
Grouping by $month aggregates all years together — useful for “which month is busiest every year?” charts. Add $year to the _id if you need per-year monthly breakdowns.
Example 4 — Month in a Specific Timezone
Extract the month in Pacific Time rather than UTC:
The object form with timezone converts the instant to the specified zone before extracting the month. Near month boundaries, UTC and local months can differ — always use timezone when business rules follow local calendars.
Bonus — Group by Year and Month Together
Build a year-month breakdown for detailed reporting:
Pairing $year and $month gives you unique year-month buckets like { year: 2024, month: 7 }. This is the foundation for monthly revenue tables and trend charts.
Applications
🚀 Use Cases
Seasonal analysis — compare summer vs winter sales using month ranges.
Monthly dashboards — group revenue or events by month of year.
Subscription billing — identify renewal months or anniversary dates.
🧠 How $month Works
1
MongoDB resolves the date
The input expression is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$month extracts the month
MongoDB reads the month as an integer 1–12 (UTC or specified timezone).
Extract
3
An integer is returned
The number is ready for $match, $group, or seasonal filters.
Output
=
📅
Month for seasonal analytics
Combine with $year, $dayOfMonth, and $dateTrunc for richer date reporting.
Wrap Up
Conclusion
The $month operator extracts the month component from BSON dates in MongoDB aggregation pipelines. It is especially useful for seasonal reports, monthly dashboards, and quarter-based filters.
Remember it returns 1–12 (not 0–11 like JavaScript), uses UTC by default, and supports an optional timezone. Next in the series: $multiply.
Pass timezone when seasonal rules follow local calendars
Use $dateTrunc when you need full monthly Date buckets
Store dates as BSON Date type for reliable extraction
❌ Don’t
Assume 0-based months like JavaScript getMonth()
Use it on string dates without converting to Date first
Forget UTC default when comparing to local fiscal periods
Confuse $month with $dateToString formatted output
Group only by month when you need per-year monthly trends (add $year)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $month
Use these points when working with monthly date extraction in MongoDB.
5
Core concepts
🕑01
1–12 Range
Jan=1, Dec=12.
Purpose
📝02
One Date Arg
Field or ISODate.
Syntax
🛠03
UTC Default
Timezone optional.
Timezone
🔄04
Not JS getMonth
1-based, not 0.
Edge case
📑05
Date Family
$year, $dayOfMonth.
Related
❓ Frequently Asked Questions
$month returns an integer from 1 to 12 representing the month of a BSON Date. January is 1, June is 6, and December is 12.
Simple form: { $month: <dateExpression> }. With timezone: { $month: { date: <dateExpression>, timezone: <tz> } }. Pass a field like "$orderedAt" or a literal ISODate.
By default, $month uses UTC. To get the month in a specific timezone, use the object form with a timezone string like "America/New_York".
$month is 1-based: January = 1, December = 12. This differs from JavaScript's Date.getMonth(), which is 0-based (January = 0).
$month returns an integer 1–12 for the month component. $dateTrunc returns a Date rounded down to a period boundary (month, day, year) — better for monthly buckets as full dates.