MongoDB $dayOfWeek Operator

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

What Is $dayOfWeek?

$dayOfWeek is a MongoDB aggregation expression that returns the weekday number for a date as an integer from 1 to 7, where 1 is Sunday and 7 is Saturday.

Remember
$dayOfWeek = weekday number from a date

{ $dayOfWeek: <dateExpression> }
{ $dayOfWeek: { date: <date>, timezone: <tz> } }

1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday

default timezone → UTC
string date → error
null / missing → null
UsePrefer
Sunday = 1 weekday number$dayOfWeek
Monday = 1 (ISO weekday)$isoDayOfWeek
Day number inside the month$dayOfMonth

How to Use $dayOfWeek in MongoDB

Pass a date field (or date + timezone), then filter or group on the weekday number if needed.

  1. Choose an expression stage ($project or $addFields).
  2. Ensure the argument is a Date / Timestamp / ObjectId (not a plain string).
  3. Pass that expression to $dayOfWeek (add timezone when you need local weekdays).
  4. Optionally filter weekends with $match on values 1 and 7.
PieceRequired?Role
$dayOfWeekYesWeekday → integer 1–7
Date expressionYesField, ISODate, Timestamp, or ObjectId
timezoneNoLocal weekday instead of UTC

Pseudocode

Pseudocode
for each document:
    d = evaluate(dateExpression)   // Date / Timestamp / ObjectId
    if d is null or missing:
        result = null
    else if d is a string:
        error
    else:
        // optional timezone shifts the calendar day first
        result = weekdayNumber(d)   // 1=Sun … 7=Sat
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while extracting weekdays.

TopicDetail
TypeAggregation expression (not a find filter alone)
Syntax{ $dayOfWeek: "$when" }
With timezone{ $dayOfWeek: { date: "$when", timezone: "America/New_York" } }
Range1 (Sunday) … 7 (Saturday)
Default TZUTC
Weekend check1 or 7
Sample check2024-01-07 (UTC Sunday) → 1

Live Preview

Watch $dayOfWeek turn a UTC date into a weekday number (Sunday = 1).

Simulates { $dayOfWeek: ISODate("…") } in UTC (same idea as the mongosh examples below).

Result
when = 2024-01-08T12:00:00.000Z
$dayOfWeek → 2 (Monday)

{ title: "Standup", dayOfWeek: 2 }

Worked Walkthrough — Why Sunday Is 1

MongoDB’s weekday scale starts on Sunday. That is different from ISO weekdays (Monday = 1).

Calculation
UTC date: 2024-01-07T12:00:00Z

Calendar weekday → Sunday

$dayOfWeek numbering:
  Sun Mon Tue Wed Thu Fri Sat
   1   2   3   4   5   6   7

$dayOfWeek(...) → 1

Compare with ISO:
  $isoDayOfWeek for the same Sunday → 7

Pick the operator that matches your report’s week start.

MongoDB Examples

We store three events on Sunday, Monday, and Saturday, extract the weekday number, then keep only weekend events. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine a tiny events collection with three UTC dates: Sunday, Monday, and Saturday.

mongosh
db.events.insertMany([
  { _id: 1, title: "Kickoff", when: ISODate("2024-01-07T12:00:00Z") },
  { _id: 2, title: "Standup", when: ISODate("2024-01-08T12:00:00Z") },
  { _id: 3, title: "Demo",    when: ISODate("2024-01-13T12:00:00Z") }
]);

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps three documents in events. Run find() to see them:

mongosh
db.events.find()

Read those documents like this:

Field in MongoDBValuePlain English
_id1 … 3Document id for this lesson
title"Kickoff", "Standup", "Demo"Event name
whenUTC ISODate valuesDate we will turn into a weekday number
Picture
Expected $dayOfWeek (UTC):

  Kickoff  2024-01-07 → Sunday    → 1
  Standup  2024-01-08 → Monday    → 2
  Demo     2024-01-13 → Saturday  → 7

How It Works

  1. Insert once. insertMany adds three event documents with real BSON dates.
  2. Dates land on different weekdays. That makes the 1 / 2 / 7 pattern easy to check.
  3. find() shows the same shape. Ready for $dayOfWeek next.

Example 2 — Extract Weekday

Goal: project each title with its $dayOfWeek number.

mongosh
db.events.aggregate([
  {
    $project: {
      title: 1,
      dayOfWeek: { $dayOfWeek: "$when" },
      _id: 0
    }
  }
]);

How It Works

  1. Read the date field. "$when" is a BSON Date from Example 1.
  2. Convert to weekday. $dayOfWeek returns 1, 2, and 7 in UTC.
  3. Keep a short report. $project keeps title and adds dayOfWeek.
  4. Need local time? Use { $dayOfWeek: { date: "$when", timezone: "Asia/Kolkata" } }.

Example 3 — Filter Weekends

Same collection: keep only Sunday (1) and Saturday (7).

mongosh
db.events.aggregate([
  {
    $addFields: {
      dayOfWeek: { $dayOfWeek: "$when" }
    }
  },
  {
    $match: {
      dayOfWeek: { $in: [1, 7] }
    }
  },
  {
    $project: {
      title: 1,
      dayOfWeek: 1,
      _id: 0
    }
  }
]);

How It Works

  1. Add the weekday first. $addFields computes dayOfWeek for every event.
  2. Keep weekends only. $match with $in: [1, 7] drops Monday.
  3. Trim the output. $project keeps title and dayOfWeek.
  4. Easy check. Kickoff (Sun) and Demo (Sat) remain; Standup (Mon) is gone.

Edge Cases & Pitfalls

Check these before using $dayOfWeek in production pipelines.

numbering

Sunday is 1, not Monday

If your product week starts on Monday, use $isoDayOfWeek instead.

timezone

UTC by default

A late evening local time can be the next UTC day. Pass timezone when the local weekday matters.

types

Strings are not dates

"2024-01-07" as a string errors. Store BSON dates, or convert with $dateFromString first.

null

Null and missing become null

Missing date fields return null, not an error. Filter them before weekend logic if nulls are invalid.

Performance Notes

ApproachCostBest for
$dayOfWeek alonePer-document date mathReports and labels
+ $match on weekdayStill scans after computeWeekend / weekday filters
Store weekday fieldExtra write / storageHeavy repeated weekday filters

Date extraction is cheap compared with large joins. Filter early with $match on the date range when you only need one week or month.

Key Takeaways

  • Role: weekday number from a date → 1–7.
  • Scale: Sunday = 1, Saturday = 7.
  • Timezone: UTC by default; pass timezone for local days.
  • ISO weeks: use $isoDayOfWeek when Monday should be 1.

One line: $dayOfWeek answers “which weekday is this date?”—as 1–7 with Sunday first.

Frequently Asked Questions

$dayOfWeek returns an integer from 1 to 7 for the weekday of a date. In MongoDB, 1 is Sunday, 2 is Monday, through 7 is Saturday. Use it inside aggregation expression stages such as $project and $addFields.
Short form: { $dayOfWeek: <dateExpression> }. Document form: { $dayOfWeek: { date: <dateExpression>, timezone: <tz> } }. The date must resolve to a Date, Timestamp, or ObjectId—not a string.
$dayOfWeek uses Sunday = 1 and Saturday = 7. $isoDayOfWeek uses Monday = 1 and Sunday = 7 (ISO-8601). Pick the numbering your reports expect.
Yes by default. Pass timezone (Olson id like "America/New_York" or an offset like "-0400") in the document form when you need a local weekday.
$dayOfWeek returns null when the input is null or the field is missing. A plain string date causes an aggregation error.
Yes. Project or compute $dayOfWeek, then keep values 1 and 7 (Sunday and Saturday) with $match and $in, or use $expr in $match.

Next: $dayOfYear

Extract the day-of-year number (1–366) from a date in aggregation.

$dayOfYear 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