MongoDB $dayOfMonth Operator

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 4 Examples
Date Operations

What You’ll Learn

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.

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.

📝 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

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (date extraction)
ArgumentsOne date expression
Return typeInteger (1–31)
TimezoneUTC (use $dateToParts for local)
MongoDB version3.4+
From field
{ $dayOfMonth: "$createdAt" }

Day of createdAt

Literal date
{ $dayOfMonth: ISODate("2024-06-15") }

Returns 15

In $project
{
  billingDay: {
    $dayOfMonth: "$renewalDate"
  }
}

Add computed field

With $month
{
  m: { $month: "$d" },
  dom: { $dayOfMonth: "$d" }
}

Month + day pair

Examples Gallery

Extract billing days, filter first-of-month events, match birthday days, and analyze activity patterns with $dayOfMonth.

📚 Extract the Day from a Date Field

Use a subscriptions collection and read the renewal day from renewalDate.

Sample Input Documents

Suppose you have a subscriptions collection with renewal dates:

mongosh
[
  {
    "_id": ObjectId("609c26812e9274a86871bc6a"),
    "plan": "Pro",
    "renewalDate": ISODate("2024-06-15T00:00:00Z")
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6b"),
    "plan": "Basic",
    "renewalDate": ISODate("2024-03-01T12:00:00Z")
  }
]

Example 1 — Add billingDay with $dayOfMonth

Extract the day number from each subscription’s renewal date:

mongosh
db.subscriptions.aggregate([
  {
    $project: {
      plan: 1,
      renewalDate: 1,
      billingDay: { $dayOfMonth: "$renewalDate" }
    }
  }
])

How It Works

  • June 15 → 15; March 1 → 1.
  • 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:

mongosh
db.users.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          { $eq: [ { $dayOfMonth: "$birthDate" }, { $dayOfMonth: "$$NOW" } ] },
          { $eq: [ { $month: "$birthDate" }, { $month: "$$NOW" } ] }
        ]
      }
    }
  },
  {
    $project: {
      name: 1,
      birthDate: 1
    }
  }
])

How It Works

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):

mongosh
db.orders.aggregate([
  {
    $group: {
      _id: { $dayOfMonth: "$orderDate" },
      orderCount: { $sum: 1 },
      totalAmount: { $sum: "$amount" }
    }
  },
  { $sort: { _id: 1 } }
])

How It Works

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.

🚀 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.

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $dayOfMonth

Use these points when extracting the day from dates in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $dayOfWeek to extract weekday numbers, or review $dateToParts for timezone-aware components.

Next: $dayOfWeek →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful