The $dayOfMonth operator extracts the day number (1–31) from a BSON Date inside MongoDB aggregation pipelines. Use it for billing cycles, first-of-month reports, birthday-day matching, or any query that depends on which day of the month an event occurred.
01
Extract Day
Date → integer 1–31.
02
Syntax
One date argument.
03
Return Type
Integer, not Date.
04
UTC Default
Uses UTC calendar.
05
Use Cases
Billing, birthdays.
06
Related Ops
$month, $dayOfWeek.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dayOfMonth operator takes a date expression and returns an integer between 1 and 31. For example, $dayOfMonth applied to ISODate("2024-06-15T14:30:00Z") returns 15. The result is always a number, not a Date or string.
💡
Beginner Tip
$dayOfMonth uses the UTC calendar. A timestamp near midnight in your local zone may show a different day than you expect — use $dateToParts with timezone when local calendar days matter.
Foundation
📝 Syntax
The $dayOfMonth operator takes a single date expression:
mongosh
{ $dayOfMonth: <dateExpression> }
Syntax Rules
Argument — any expression that evaluates to a BSON Date (field, $$NOW, or literal).
Return value — integer from 1 to 31 representing the day in UTC.
Null input — returns null when the date is null or missing.
Use inside $project, $addFields, $set, $match (with aggregation syntax), or $group.
Pair with $month and $year for full calendar filtering.
💡 $dayOfMonth vs $dateToParts vs $dateTrunc
$dayOfMonth — BSON Date → single integer (day 1–31)
$dateToParts — BSON Date → object with year, month, day, etc.
$dateTrunc — BSON Date → BSON Date at period start
The operator ignores time-of-day for the day number (both return the calendar day in UTC).
Store or index billingDay if you query by it often.
📈 Practical Patterns
First-of-month filters, birthday matching, and grouping by day of month.
Example 2 — Find Renewals on the 1st of the Month
Filter subscriptions that renew on day 1 using $expr and $dayOfMonth:
mongosh
db.subscriptions.find({
$expr: {
$eq: [
{ $dayOfMonth: "$renewalDate" },
1
]
}
})
// Returns documents where renewalDate falls on UTC day 1
How It Works
In a standard find query, wrap aggregation expressions in $expr. This pattern works for any day number — replace 1 with 15 for mid-month billing, for example.
Example 3 — Match Birthday Day (Any Year)
Combine $dayOfMonth with $month to find users with birthdays today:
Matching on month and day ignores the birth year, so you can send birthday emails or promotions. For local-time birthdays, extract parts with $dateToParts and a timezone instead.
Example 4 — Group Orders by Day of Month
Analyze whether orders cluster on certain days (e.g. paydays):
Using $dayOfMonth directly in $group._id buckets all orders from the 1st across every month into one group. Combine with $month or $year in _id when you need finer breakdowns.
Applications
🚀 Use Cases
Subscription billing — identify which day of the month each customer renews.
Payroll and invoicing — filter transactions that occur on the 1st or 15th.
Birthday campaigns — match month and day while ignoring year.
Seasonal patterns — see if activity spikes on month-end days (28–31).
🧠 How $dayOfMonth Works
1
MongoDB resolves the date
The input expression is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dayOfMonth reads the UTC calendar
MongoDB determines which day of the month the instant falls on in UTC.
Extract
3
An integer is returned
The value 1–31 is available for filtering, grouping, or display alongside other date parts.
Output
=
📅
Day number for queries
Combine with $month, $year, or $dayOfWeek for rich calendar logic in pipelines.
Wrap Up
Conclusion
The $dayOfMonth operator is a simple, fast way to pull the day number from BSON dates in MongoDB aggregation pipelines. One expression replaces manual date parsing for billing days, calendar filters, and day-of-month analytics.
Remember it uses UTC. For timezone-aware extraction, use $dateToParts. For full period bucketing, prefer $dateTrunc. Next in the series: $dayOfWeek for weekday extraction.
Combine with $month for birthday or anniversary matching
Use $dateToParts + timezone for local calendar days
Store BSON dates, not strings, for reliable extraction
Use $expr in find() when you need $dayOfMonth
Add $year to $group._id when you need per-month buckets
❌ Don’t
Assume UTC day matches the user’s local day near midnight
Confuse $dayOfMonth with $dayOfWeek (1–7 weekday)
Expect a leading zero — the result is an integer, not a string
Group by day alone when you mean “day within each month”
Parse string dates — convert with $dateFromString first
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dayOfMonth
Use these points when extracting the day from dates in MongoDB.
5
Core concepts
📅01
Extract Day
Date → 1–31.
Purpose
📝02
One Argument
Date expression.
Syntax
🛠03
Integer Output
Not a Date.
Output
🌐04
UTC Calendar
Default timezone.
Note
⚠05
Pair with $month
Full date parts.
Important
❓ Frequently Asked Questions
$dayOfMonth returns an integer from 1 to 31 representing the day of the month for a BSON Date. For example, June 15 returns 15. It is used inside aggregation expressions such as $project and $addFields.
{ $dayOfMonth: <dateExpression> }. Pass a field reference like "$createdAt", a literal ISODate, or another expression that evaluates to a date.
$dayOfMonth extracts the day in UTC. If you need the day in a specific timezone, use $dateToParts with a timezone field instead.
$dayOfMonth returns a single integer (the day). $dateToParts returns a full object with year, month, day, hour, and other components.
$dayOfMonth is available in MongoDB 3.4 and later in aggregation pipelines and update pipelines that use aggregation expressions.