$dayOfMonth (Get Day of Month)
Last updated: 2026-10-09
$dayOfMonth (Get Day of Month)
$dayOfMonth returns the day of month of a date (1 to 31) — a common expression for day-level stats. It accepts either a plain date expression or an object with an explicit timezone. Typical uses: grouping orders by day, building day labels, or detecting month starts. It returns an integer; the day may differ across zones, so always set timezone (defaults to UTC).
| Category | Date |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $dayOfMonth: { date: <date-expression>, timezone: <timezone> } }
⚙️ Parameters
| date | Required; the date expression. |
|---|---|
| timezone | Optional timezone; defaults to UTC. |
Returns:The day of month (1-31) as an integer; null if date is null.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $dayOfMonth: get order day (Beijing time)
db.orders.aggregate([
{ $project: {
day: { $dayOfMonth: { date: '$orderDate', timezone: 'Asia/Shanghai' } }
} }
])
Output:
{ "_id": 1, "day": 25 }
{ "_id": 2, "day": 1 }
📌 Notes
- Integer 1-31 without zero padding; use %d of $dateToString for two digits
- Unlike $dayOfWeek and $dayOfYear, this is the day within the month
- null or missing date yields null
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QWhat differs between $dayOfMonth and %d?
A%d outputs a two-digit string (05); $dayOfMonth outputs an integer (5).
QDoes it start at 0 or 1?
AStarts at 1 — the 1st of a month is 1.
QDoes timezone affect the result?
AYes — UTC and local zones can give different days; set timezone explicitly.