MongoDB $isoWeek Operator

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

What You’ll Learn

The $isoWeek operator extracts the ISO 8601 week number (1–53) from a date. Use it in aggregation pipelines to build weekly sales reports, group events by ISO week, or filter records for a specific week of the year.

01

ISO Week Number

Date → integer 1–53.

02

Syntax

One date argument.

03

Monday Start

ISO 8601 calendar.

04

$group Stage

Weekly aggregations.

05

Use Cases

Reports, dashboards.

06

Pair $isoWeekYear

Year-week keys.

Definition and Usage

In MongoDB’s aggregation framework, the $isoWeek operator takes a date expression and returns the ISO 8601 week number as an integer from 1 to 53. ISO weeks start on Monday, and week 1 is the first week that contains a Thursday (the week that includes January 4). For example, $isoWeek on ISODate("2024-06-17T00:00:00Z") returns 25.

Weekly business reports often use ISO week numbers because they align with international standards. Combine $isoWeek with $isoWeekYear when you need a unique year-week identifier, especially for dates near January or December where the ISO week may belong to a different calendar year.

💡
Beginner Tip

Do not use calendar month alone when you need “week of year” reporting. ISO week boundaries do not match month boundaries. For example, January 1 can fall in week 52 or 53 of the previous ISO year.

📝 Syntax

The $isoWeek operator takes a single date expression:

mongosh
{ $isoWeek: <dateExpression> }

Syntax Rules

  • Argument — any expression that evaluates to a BSON Date.
  • Return value — integer 1 through 53 (ISO week number in UTC).
  • Null input — returns null when the date is null or missing.
  • Use inside $project, $addFields, $match with $expr, or $group.
  • Pair with $isoWeekYear for complete ISO year-week grouping.

💡 Year Boundary Edge Cases

Calendar year and ISO week year can differ near January and December:

2024-01-01 (Monday) → ISO week 1 of 2024
2023-01-01 (Sunday) → ISO week 52 of 2022
Safe grouping key{ year: { $isoWeekYear: "$date" }, week: { $isoWeek: "$date" } }

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (date extraction)
Syntax{ $isoWeek: <dateExpression> }
Return typeInteger 1–53 (ISO week number)
Week starts onMonday (ISO 8601)
Common stages$project, $group, $match + $expr
Field
{
  $isoWeek: "$orderDate"
}

Week from field

Literal date
{
  $isoWeek: ISODate(
    "2024-06-17"
  )
}

Returns 25

$group key
{
  _id: {
    $isoWeek: "$date"
  }
}

Group by week

Null input
{
  $isoWeek: null
}

Returns null

Examples Gallery

Walk through sample order data, extract ISO week numbers with $project, filter a specific week, and build weekly revenue reports with $group.

📚 Extract ISO Week Number

Start with an orders collection and add an ISO week number to each document.

Sample Input Documents

Suppose you have an orders collection with orderDate and amount fields:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "orderDate": ISODate("2024-06-17T14:30:00Z"), "amount": 120 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "orderDate": ISODate("2024-06-20T10:00:00Z"), "amount": 85 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "orderDate": ISODate("2024-06-24T18:45:00Z"), "amount": 200 }
]

June 17 and June 20 fall in ISO week 25; June 24 falls in ISO week 26.

Example 1 — Basic $isoWeek on a Field

Add an isoWeek field to each order:

mongosh
db.orders.aggregate([
  {
    $project: {
      orderDate: 1,
      amount: 1,
      isoWeek: {
        $isoWeek: "$orderDate"
      }
    }
  }
])

How It Works

  • Both June 17 and June 20 share the same ISO week (25) because they fall in the same Monday–Sunday span.
  • June 24 starts a new ISO week (26) because it is the following Monday.
  • Results are based on the UTC calendar date of the stored ISODate.

📈 Practical Patterns

Filter orders for a specific week, build year-week keys, and aggregate weekly revenue.

Example 2 — Filter Orders in ISO Week 25

Find all orders from ISO week 25 using $match and $expr:

mongosh
db.orders.aggregate([
  {
    $match: {
      $expr: {
        $eq: [
          { $isoWeek: "$orderDate" },
          25
        ]
      }
    }
  },
  {
    $project: {
      orderDate: 1,
      amount: 1,
      isoWeek: {
        $isoWeek: "$orderDate"
      }
    }
  }
])

How It Works

For year-boundary weeks, add $isoWeekYear to the filter so week 1 of 2025 is not mixed with week 1 of 2024.

Example 3 — Build a Year-Week Key

Combine $isoWeekYear and $isoWeek for a unique weekly identifier:

mongosh
db.orders.aggregate([
  {
    $project: {
      orderDate: 1,
      amount: 1,
      yearWeek: {
        $concat: [
          { $toString: { $isoWeekYear: "$orderDate" } },
          "-W",
          {
            $cond: [
              { $lt: [ { $isoWeek: "$orderDate" }, 10 ] },
              { $concat: [ "0", { $toString: { $isoWeek: "$orderDate" } } ] },
              { $toString: { $isoWeek: "$orderDate" } }
            ]
          }
        ]
      }
    }
  }
])

How It Works

The padded week number (e.g. W05) follows common ISO week label formats like 2024-W25. This prevents collisions when the same week number appears in different ISO years.

Example 4 — Group Revenue by ISO Week

Summarize total sales for each week:

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

How It Works

Grouping by both $isoWeek and $isoWeekYear produces accurate weekly buckets even when dates span year boundaries. This is the standard pattern for weekly KPI dashboards.

Bonus — Year Boundary Example

See how a January date can belong to the previous ISO year’s final week:

mongosh
db.events.aggregate([
  {
    $project: {
      eventDate: 1,
      calendarYear: { $year: "$eventDate" },
      isoWeek: { $isoWeek: "$eventDate" },
      isoWeekYear: { $isoWeekYear: "$eventDate" }
    }
  }
])

// Input: ISODate("2023-01-01T00:00:00Z")
// calendarYear: 2023, isoWeek: 52, isoWeekYear: 2022

How It Works

January 1, 2023 was a Sunday and falls in ISO week 52 of 2022. Using $year alone would mis-group this event. Always pair $isoWeek with $isoWeekYear near year boundaries.

🚀 Use Cases

  • Weekly sales reports — group revenue, orders, or signups by ISO week for trend analysis.
  • Operations dashboards — compare week-over-week metrics using standard ISO week numbers.
  • Time-based filtering — isolate events or transactions for a specific week of the year.
  • ISO calendar pipelines — combine with $isoDayOfWeek and $isoWeekYear for full weekly analysis.

🧠 How $isoWeek Works

1

MongoDB reads the date expression

The pipeline evaluates the input — a field like "$orderDate" or an ISODate literal.

Input
2

$isoWeek applies ISO 8601 rules

MongoDB determines which Monday-starting week the date falls in and assigns the ISO week number (1–53).

Calculate
3

The week number is stored

The integer is written to your output field or used as part of a $group key for weekly aggregation.

Output
=

Weekly reporting data

You get standard ISO week numbers ready for grouping, filtering, and dashboard charts.

Conclusion

The $isoWeek operator is essential for ISO 8601 weekly reporting in MongoDB aggregation pipelines. It turns dates into week numbers 1–53, enabling clean weekly grouping and filtering without manual date math.

For beginners, the key idea is simple: wrap any date expression in { $isoWeek: ... } inside a stage like $project or $group. Always pair it with $isoWeekYear when dates may fall near January or December, and remember that ISO weeks start on Monday.

💡 Best Practices

✅ Do

  • Pair $isoWeek with $isoWeekYear for grouping keys
  • Use ISO week numbers for international weekly reports
  • Sort by year then week when displaying weekly time series
  • Filter with $expr and $eq for specific weeks
  • Combine with $isoDayOfWeek for day-within-week analysis

❌ Don’t

  • Use calendar $month alone when you need ISO week grouping
  • Assume week 1 always starts on January 1
  • Forget year-boundary edge cases in January and December
  • Use on string dates without converting to BSON Date first
  • Confuse $isoWeek with $week (non-ISO numbering)

Key Takeaways

Knowledge Unlocked

Five things to remember about $isoWeek

Use these points when building weekly reports in MongoDB.

5
Core concepts
📝 02

Simple Syntax

{ $isoWeek: date }

Syntax
🛠 03

$group Weekly

Revenue by week.

Usage
📈 04

+ $isoWeekYear

Unique year-week key.

Pattern
05

Year Edges

Jan/Dec can surprise.

Edge case

❓ Frequently Asked Questions

$isoWeek returns the ISO 8601 week number (1–53) for a BSON Date. It is an aggregation expression operator used inside stages like $project, $addFields, and $group.
The syntax is { $isoWeek: <dateExpression> }. Pass a field reference like "$orderDate", a literal ISODate, or another expression that evaluates to a date.
ISO weeks start on Monday. Week 1 is the first week of the year that contains a Thursday (equivalently, the week containing January 4). Some dates near year boundaries belong to week 52 or 53 of the previous year, or week 1 of the next.
Yes, when grouping or filtering by week. A date in early January might be ISO week 1 of the new year while still being in the previous calendar year, or vice versa. Pair both operators for accurate year-week keys.
$isoWeek returns null when the input date is null or refers to a missing field. It does not throw an error.

Continue the Operator Series

Move on to $isoWeekYear for ISO week-year values, or review $isoDayOfWeek for weekday analysis.

Next: $isoWeekYear →

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