MongoDB $dayOfMonth Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $dayOfMonth?

$dayOfMonth is a MongoDB aggregation expression that returns the day of the month as an integer from 1 to 31. Default timezone is UTC; you can pass a timezone when local day matters.

Remember
$dayOfMonth = calendar day number (1–31)

{ $dayOfMonth: <dateExpression> }

// or with timezone:
{ $dayOfMonth: { date: <date>, timezone: <string> } }

input  → Date / Timestamp / ObjectId (not a string)
output → integer 1 … 31
default zone → UTC

null / missing → null
string date → error (parse with $dateFromString first)
UsePrefer
Day number only (1–31)$dayOfMonth
Weekday number$dayOfWeek
Full year/month/day object$dateToParts

How to Use $dayOfMonth in MongoDB

Point at a Date field, optionally set a timezone, then use the integer in reports or filters.

  1. Choose an expression stage ($addFields or $project).
  2. Pass a Date field: { $dayOfMonth: "$starts_at" }.
  3. For local day, use { $dayOfMonth: { date: "$starts_at", timezone: "America/New_York" } }.
  4. Optionally filter with $match + $expr on that day number.
PieceRequired?Role
Date expressionYesDate / Timestamp / ObjectId to read
timezoneNoLocal day instead of UTC
Result—Integer 1–31

Pseudocode

Pseudocode
for each document:
    d = evaluate(dateExpression)
    if d is null or missing:
        result = null
    else:
        result = day-of-month of d in timezone (default UTC)
                // integer from 1 to 31
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while extracting day-of-month values.

TopicDetail
TypeAggregation expression (not a find filter)
Short syntax{ $dayOfMonth: "$starts_at" }
With timezone{ $dayOfMonth: { date: "$starts_at", timezone: "UTC" } }
Range1 … 31
Default zoneUTC
Related$month, $dayOfWeek
Sample check2026-10-02 → 2

Live Preview

Watch $dayOfMonth return the calendar day number (UTC) for a few dates.

Simulates $dayOfMonth in UTC (same idea as the mongosh examples below).

Result
date = ISODate("2026-10-02T09:15:00.000Z")

$dayOfMonth → 2

{ day: 2 }

Worked Walkthrough — Day 2

An event starts on 2026-10-02T09:15:00Z. We want the day number from that Date.

Calculation
Document:
  { starts_at: ISODate("2026-10-02T09:15:00.000Z") }

$dayOfMonth: "$starts_at"

Step by step (UTC):
  year  → 2026
  month → 10 (October)
  day   → 2

Result:
  2

Check: October 2 → day-of-month is 2 ✓

MongoDB Examples

We use one event with a start time. First we store it, then we extract the day number, then we filter for events that start on that day of the month. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one event named Launch that starts at 2026-10-02 09:15 UTC. We save that Date in a collection named events.

mongosh
db.events.insertOne({
  _id: 1,
  title: "Launch",
  starts_at: ISODate("2026-10-02T09:15:00.000Z")
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in events. Run find() to see it:

mongosh
db.events.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
title"Launch"Event name
starts_atISODate("2026-10-02T09:15:00.000Z")Start time (real Date)
Picture
Date:

  2026-10-02 09:15 UTC
         |
         v  $dayOfMonth
         2

Only the day number — not month, not weekday.

How It Works

  1. Insert once. insertOne adds a single event document to events.
  2. Store a real Date. ISODate(...) creates a BSON Date, not a string.
  3. find() shows the same shape. What you inserted is what you get back—ready for $dayOfMonth next.

Example 2 — Extract Day

Goal: add a day field with the day-of-month integer.

mongosh
db.events.aggregate([
  {
    $addFields: {
      day: { $dayOfMonth: "$starts_at" }
    }
  }
]);

How It Works

  1. Read the stored Date. MongoDB already has starts_at from Example 1.
  2. Extract in UTC. Short form { $dayOfMonth: "$starts_at" } uses UTC by default.
  3. Get an integer. October 2 → day: 2.
  4. Keep all fields. $addFields adds day next to starts_at—nothing is deleted.

Example 3 — Filter by Day

Keep only events whose start day-of-month is 2 (useful for “everything that falls on the 2nd”).

mongosh
db.events.aggregate([
  {
    $match: {
      $expr: {
        $eq: [ { $dayOfMonth: "$starts_at" }, 2 ]
      }
    }
  },
  {
    $project: {
      title: 1,
      day: { $dayOfMonth: "$starts_at" }
    }
  }
]);

How It Works

  1. Same stored document. Still using starts_at from Example 1.
  2. Compare inside $expr. Aggregation operators like $dayOfMonth need $expr inside $match.
  3. Keep matches only. Day equals 2, so Launch stays in the result.
  4. Project a clean shape. Output shows title and the day number.

Edge Cases & Pitfalls

Check these before using $dayOfMonth in production pipelines.

type

Strings are not allowed

Pass a Date, not "2026-10-02". Parse text first with $dateFromString.

tz

Default day is UTC

Near midnight, local day can differ from UTC. Use the document form with timezone when local calendars matter.

name

Not the same as $dayOfWeek

$dayOfMonth is 1–31 on the calendar. $dayOfWeek is weekday (Sunday–Saturday numbering).

null

Null date becomes null

Missing or null dates return null, not an error. Handle them before grouping or filtering if needed.

Performance Notes

ApproachCostBest for
$dayOfMonth alonePer-document extractReports and computed fields
$match + $exprScan + expressionFiltering by day number
Store day ahead of timeWrite cost, cheaper filtersVery frequent day-based queries

Extracting a day number is cheap compared with large sorts or joins. Filter early with $match when only some documents need the day field.

Key Takeaways

  • Role: Date → day of month integer (1–31).
  • Default zone: UTC (optional timezone available).
  • Input: Date only—not a date string.
  • Filter tip: use $match + $expr with $eq.

One line: $dayOfMonth answers “which day of the month is this Date?”—as a number from 1 to 31.

Frequently Asked Questions

$dayOfMonth returns an integer from 1 to 31 for the day of the month of a BSON Date. For example, 2026-10-02 returns 2. Use it inside aggregation stages such as $project and $addFields.
Short form: { $dayOfMonth: <dateExpression> }. With timezone: { $dayOfMonth: { date: <dateExpression>, timezone: <string> } }. Without timezone, the day is in UTC.
Default is UTC. Pass timezone (Olson ID or UTC offset) when you need the day as it appears in a local zone—especially near midnight.
$dayOfMonth returns one integer (the day). $dateToParts returns a full document with year, month, day, hour, and more.
No. It needs a Date, Timestamp, or ObjectId. Parse text first with $dateFromString.
Yes in spirit for the UTC case. Think of $dayOfMonth as MongoDB’s aggregation version of reading the calendar day—run it in the pipeline, not as a standalone find() filter.

Next: $dayOfWeek

Extract the day of the week number from a BSON Date.

$dayOfWeek tutorial →

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.

9 people found this page helpful