MongoDB $dayOfWeek Operator

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

What You’ll Learn

The $dayOfWeek operator extracts the weekday as an integer from 1 to 7 inside MongoDB aggregation pipelines. Use it to filter weekend traffic, compare Monday vs Friday sales, or group events by weekday pattern.

01

Extract Weekday

Date → integer 1–7.

02

Syntax

One date argument.

03

Sunday = 1

MongoDB numbering.

04

UTC Default

Uses UTC calendar.

05

Use Cases

Weekends, weekdays.

06

vs $isoDayOfWeek

Different numbering.

Definition and Usage

In MongoDB’s aggregation framework, the $dayOfWeek operator takes a date expression and returns an integer from 1 to 7. MongoDB numbers weekdays with Sunday = 1 through Saturday = 7. For example, $dayOfWeek on ISODate("2024-06-15T00:00:00Z") (a Saturday) returns 7.

💡
Beginner Tip

Do not assume Monday = 1. MongoDB’s $dayOfWeek starts the week on Sunday. For ISO-style numbering (Monday = 1), use $isoDayOfWeek instead.

📝 Syntax

The $dayOfWeek operator takes a single date expression:

mongosh
{ $dayOfWeek: <dateExpression> }

Syntax Rules

  • Argument — any expression that evaluates to a BSON Date.
  • Return value — integer 1 (Sunday) through 7 (Saturday) in UTC.
  • Null input — returns null when the date is null or missing.
  • Use inside $project, $addFields, $match with $expr, or $group.

Weekday Number Reference

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

💡 $dayOfWeek vs $isoDayOfWeek vs $dayOfMonth

$dayOfWeek — Sunday = 1, Saturday = 7
$isoDayOfWeek — Monday = 1, Sunday = 7 (ISO 8601)
$dayOfMonth — day of month 1–31, not weekday

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (date extraction)
ArgumentsOne date expression
Return typeInteger (1–7)
Week starts onSunday (= 1)
MongoDB version3.4+
From field
{ $dayOfWeek: "$orderDate" }

Weekday of orderDate

Saturday
{ $dayOfWeek: ISODate("2024-06-15") }

Returns 7

Weekend filter
{ $in: [
  { $dayOfWeek: "$d" },
  [1, 7]
]}

Sun or Sat

Monday only
{ $eq: [
  { $dayOfWeek: "$d" },
  2
]}

Monday = 2

Examples Gallery

Extract weekday numbers, filter weekend orders, find Monday activity, and analyze sales by day of week with $dayOfWeek.

📚 Extract the Weekday from a Date

Use an orders collection and add a weekday number from orderDate.

Sample Input Documents

Suppose you have an orders collection with order timestamps:

mongosh
[
  {
    "_id": ObjectId("609c26812e9274a86871bc6a"),
    "item": "Laptop",
    "orderDate": ISODate("2024-06-15T14:30:00Z")
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6b"),
    "item": "Desk",
    "orderDate": ISODate("2024-06-17T10:00:00Z")
  }
]

Example 1 — Add weekday with $dayOfWeek

Extract the weekday integer from each order date:

mongosh
db.orders.aggregate([
  {
    $project: {
      item: 1,
      orderDate: 1,
      weekday: { $dayOfWeek: "$orderDate" }
    }
  }
])

How It Works

  • June 15, 2024 is a Saturday → 7.
  • June 17, 2024 is a Monday → 2.
  • Remember: Sunday = 1, not Monday.

📈 Practical Patterns

Weekend filters, weekday-only reports, and sales breakdowns by day of week.

Example 2 — Filter Weekend Orders

Find orders placed on Saturday or Sunday using $expr and $in:

mongosh
db.orders.find({
  $expr: {
    $in: [
      { $dayOfWeek: "$orderDate" },
      [1, 7]
    ]
  }
})

// 1 = Sunday, 7 = Saturday

How It Works

Weekend detection in MongoDB uses [1, 7] with $dayOfWeek, not Saturday/Sunday as 6 and 7 in ISO numbering. Double-check your operator before writing filters.

Example 3 — Find Monday Transactions

Filter documents where the event occurred on a Monday (weekday = 2):

mongosh
db.events.aggregate([
  {
    $match: {
      $expr: {
        $eq: [
          { $dayOfWeek: "$eventAt" },
          2
        ]
      }
    }
  },
  {
    $project: {
      title: 1,
      eventAt: 1
    }
  }
])

How It Works

Use the weekday reference table when building filters. Monday = 2, Friday = 6. Swap to $isoDayOfWeek if your team expects ISO Monday-first numbering.

Example 4 — Group Sales by Day of Week

See which weekdays drive the most revenue:

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

// _id: 1 → Sunday totals, _id: 7 → Saturday totals

How It Works

Grouping by $dayOfWeek aggregates all Sundays across every week into one bucket. Add $week or $year to _id when you need per-week breakdowns.

🚀 Use Cases

  • Retail analytics — compare weekday vs weekend purchase volume.
  • Support staffing — find which weekdays generate the most tickets.
  • Marketing schedules — target campaigns on high-traffic weekdays.
  • Compliance windows — filter events that must occur on business days (Mon–Fri).

🧠 How $dayOfWeek Works

1

MongoDB resolves the date

The input expression is evaluated per document — a field, $$NOW, or literal ISODate.

Input
2

$dayOfWeek maps to 1–7

MongoDB determines the weekday in UTC using Sunday = 1 through Saturday = 7.

Extract
3

An integer is returned

The number is ready for $match, $group, or conditional logic in pipelines.

Output
=

Weekday number for queries

Combine with $dayOfMonth, $month, or $isoDayOfWeek for richer calendar logic.

Conclusion

The $dayOfWeek operator is a simple way to extract weekday numbers from BSON dates in MongoDB aggregation pipelines. Its Sunday-first numbering is the main detail beginners must remember when writing weekend or weekday filters.

For ISO Monday-first numbering, use $isoDayOfWeek. For timezone-aware weekdays, use $dateToParts with a timezone field. Next in the series: $dayOfYear for day-of-year extraction.

💡 Best Practices

✅ Do

  • Keep a weekday cheat sheet: Sunday = 1, Monday = 2, … Saturday = 7
  • Use $isoDayOfWeek when your team expects ISO numbering
  • Filter weekends with $in: [1, 7]
  • Store BSON dates for reliable weekday extraction
  • Document numbering in shared query libraries

❌ Don’t

  • Assume Monday = 1 with $dayOfWeek
  • Confuse $dayOfWeek with $dayOfMonth
  • Forget UTC vs local time near midnight boundaries
  • Mix $dayOfWeek and $isoDayOfWeek in one dashboard without labels
  • Parse string dates — convert with $dateFromString first

Key Takeaways

Knowledge Unlocked

Five things to remember about $dayOfWeek

Use these points when extracting weekdays from dates in MongoDB.

5
Core concepts
📝 02

One Argument

Date expression.

Syntax
🛠 03

Sunday = 1

Key numbering rule.

Output
🌐 04

UTC Calendar

Default timezone.

Note
05

Not $isoDayOfWeek

Monday-first vs Sun-first.

Important

❓ Frequently Asked Questions

$dayOfWeek returns an integer from 1 to 7 representing the day of the week for a BSON Date. In MongoDB, 1 is Sunday, 2 is Monday, through 7 is Saturday.
{ $dayOfWeek: <dateExpression> }. Pass a field reference like "$orderDate", a literal ISODate, or another expression that evaluates to a date.
MongoDB uses 1 = Sunday, 2 = Monday, 3 = Tuesday, 4 = Wednesday, 5 = Thursday, 6 = Friday, 7 = Saturday. This differs from $isoDayOfWeek, where Monday = 1 and Sunday = 7.
$dayOfWeek extracts the weekday in UTC. For a specific timezone, use $dateToParts with a timezone field and read isoDayOfWeek, or use $isoDayOfWeek with timezone-aware date handling.
$dayOfWeek 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 $dayOfYear to extract the day number within a year, or review $dayOfMonth for calendar-day extraction.

Next: $dayOfYear →

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