MongoDB $isoWeekYear Operator

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

What You’ll Learn

The $isoWeekYear operator extracts the ISO week-numbering year from a date. It answers “which ISO year owns this week?” — which can differ from the calendar year near January and December. Pair it with $isoWeek for accurate weekly reporting.

01

ISO Week Year

Year for ISO weeks.

02

Syntax

One date argument.

03

vs $year

Not always the same.

04

$group Stage

Year-week buckets.

05

Use Cases

Weekly KPI reports.

06

Pair $isoWeek

Complete week keys.

Definition and Usage

In MongoDB’s aggregation framework, the $isoWeekYear operator takes a date expression and returns the ISO 8601 week-numbering year as an integer. This is the year that the ISO week belongs to — not necessarily the same as the calendar year shown on the date. For example, ISODate("2023-01-01T00:00:00Z") has calendar year 2023 but $isoWeekYear returns 2022 because that Sunday falls in ISO week 52 of 2022.

Use $isoWeekYear together with $isoWeek whenever you group, filter, or label data by week. Without it, week 1 in January could be mixed with week 1 from a different ISO year.

💡
Beginner Tip

Think of $year as “what year is on the calendar” and $isoWeekYear as “what ISO year owns this week.” For most mid-year dates they match; near January and December they often do not.

📝 Syntax

The $isoWeekYear operator takes a single date expression:

mongosh
{ $isoWeekYear: <dateExpression> }

Syntax Rules

  • Argument — any expression that evaluates to a BSON Date.
  • Return value — integer year in the ISO week-numbering system (UTC).
  • Null input — returns null when the date is null or missing.
  • Use inside $project, $addFields, $match with $expr, or $group.
  • Always combine with $isoWeek for week-based grouping or filtering.

💡 $isoWeekYear vs $year

The same date can return different years:

2023-01-01$year = 2023, $isoWeekYear = 2022
2024-12-30 (Monday)$year = 2024, $isoWeekYear = 2025 (week 1)
2024-06-17 → both return 2024 (mid-year dates usually match)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (date extraction)
Syntax{ $isoWeekYear: <dateExpression> }
Return typeInteger (ISO week-numbering year)
Pair with$isoWeek for complete week keys
Common stages$project, $group, $match + $expr
Field
{
  $isoWeekYear: "$orderDate"
}

ISO year from field

Year-week key
{
  year: {
    $isoWeekYear: "$date"
  },
  week: {
    $isoWeek: "$date"
  }
}

Safe $group _id

Compare years
{
  calendarYear: {
    $year: "$date"
  },
  isoWeekYear: {
    $isoWeekYear: "$date"
  }
}

Spot mismatches

Null input
{
  $isoWeekYear: null
}

Returns null

Examples Gallery

Walk through sample event data, compare calendar year vs ISO week year, and build accurate weekly aggregation keys.

📚 Extract ISO Week Year

Start with an events collection and add the ISO week-numbering year to each document.

Sample Input Documents

Suppose you have an events collection with dates near a year boundary:

mongosh
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "New Year Event", "eventDate": ISODate("2023-01-01T00:00:00Z"), "attendees": 50 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Mid-Year Meetup", "eventDate": ISODate("2024-06-17T14:30:00Z"), "attendees": 120 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Year-End Gala", "eventDate": ISODate("2024-12-30T18:00:00Z"), "attendees": 200 }
]

Example 1 — Basic $isoWeekYear on a Field

Add isoWeekYear and isoWeek to each event:

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

How It Works

  • January 1, 2023 belongs to ISO week 52 of 2022, not 2023.
  • December 30, 2024 (a Monday) starts ISO week 1 of 2025.
  • Mid-year dates like June 17 usually have matching calendar and ISO week years.

📈 Practical Patterns

Compare with calendar year, filter by ISO year-week, and aggregate metrics accurately.

Example 2 — Compare $year and $isoWeekYear

Spot dates where calendar year and ISO week year disagree:

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

How It Works

The yearsMatch flag highlights boundary dates that need $isoWeekYear instead of $year for weekly reporting.

Example 3 — Filter by ISO Year and Week

Find events in ISO week 1 of 2025:

mongosh
db.events.aggregate([
  {
    $match: {
      $expr: {
        $and: [
          { $eq: [ { $isoWeekYear: "$eventDate" }, 2025 ] },
          { $eq: [ { $isoWeek: "$eventDate" }, 1 ] }
        ]
      }
    }
  },
  {
    $project: {
      name: 1,
      eventDate: 1,
      isoWeekYear: { $isoWeekYear: "$eventDate" },
      isoWeek: { $isoWeek: "$eventDate" }
    }
  }
])

How It Works

Filtering only by $isoWeek: 1 would incorrectly include week 1 from other ISO years. Adding $isoWeekYear makes the filter precise.

Example 4 — Group Attendees by ISO Year-Week

Aggregate total attendees per ISO year and week:

mongosh
db.events.aggregate([
  {
    $group: {
      _id: {
        isoWeekYear: { $isoWeekYear: "$eventDate" },
        isoWeek: { $isoWeek: "$eventDate" }
      },
      totalAttendees: { $sum: "$attendees" },
      eventCount: { $sum: 1 }
    }
  },
  {
    $sort: {
      "_id.isoWeekYear": 1,
      "_id.isoWeek": 1
    }
  }
])

How It Works

Each bucket is uniquely identified by ISO year + week. The New Year event lands in 2022-W52, not 2023-W52, which is the correct ISO grouping.

Bonus — Format an ISO Year-Week Label

Build a readable label like 2025-W01 for dashboards:

mongosh
db.events.aggregate([
  {
    $project: {
      name: 1,
      eventDate: 1,
      isoYearWeek: {
        $concat: [
          { $toString: { $isoWeekYear: "$eventDate" } },
          "-W",
          {
            $cond: [
              { $lt: [ { $isoWeek: "$eventDate" }, 10 ] },
              { $concat: [ "0", { $toString: { $isoWeek: "$eventDate" } } ] },
              { $toString: { $isoWeek: "$eventDate" } }
            ]
          }
        ]
      }
    }
  }
])

How It Works

Using $isoWeekYear (not $year) in the label ensures 2024-12-30 displays as 2025-W01, matching international ISO week conventions.

🚀 Use Cases

  • Weekly KPI dashboards — group metrics by ISO year-week without boundary errors.
  • Year-boundary filtering — select events in a specific ISO week of a specific ISO year.
  • Data validation — compare $year vs $isoWeekYear to audit date-heavy imports.
  • ISO calendar pipelines — complete the trio with $isoWeek and $isoDayOfWeek.

🧠 How $isoWeekYear Works

1

MongoDB reads the date expression

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

Input
2

$isoWeekYear finds the ISO year

MongoDB determines which ISO week-numbering year contains the date’s ISO week, using ISO 8601 rules in UTC.

Calculate
3

The year integer is stored

The ISO week year is written to your output field or combined with $isoWeek in a $group key.

Output
=

Accurate year-week data

You get correct ISO year identifiers for weekly reports, even across calendar year boundaries.

Conclusion

The $isoWeekYear operator completes the ISO weekly calendar toolkit in MongoDB aggregation pipelines. It provides the correct year for ISO week grouping — essential whenever you use $isoWeek and especially critical for dates in early January or late December.

For beginners, remember: wrap any date expression in { $isoWeekYear: ... } and always pair it with $isoWeek for week-based keys. Do not substitute calendar $year when ISO week accuracy matters.

💡 Best Practices

✅ Do

  • Always pair $isoWeekYear with $isoWeek in $group keys
  • Use both operators when filtering a specific ISO year-week
  • Compare with $year to audit boundary dates in imports
  • Sort weekly reports by ISO year then ISO week
  • Format labels as YYYY-Www using $isoWeekYear

❌ Don’t

  • Replace $isoWeekYear with calendar $year for weekly grouping
  • Filter by $isoWeek alone without the ISO year
  • Assume January dates belong to the same ISO year as the calendar
  • Use on string dates without converting to BSON Date first
  • Forget UTC when comparing with local-time business rules

Key Takeaways

Knowledge Unlocked

Five things to remember about $isoWeekYear

Use these points for accurate ISO weekly reporting.

5
Core concepts
📝 02

Simple Syntax

{ $isoWeekYear: date }

Syntax
🛠 03

Pair $isoWeek

Complete week keys.

Usage
04

vs $year

Differs at boundaries.

Important
📈 05

$group Key

year + week together.

Pattern

❓ Frequently Asked Questions

$isoWeekYear returns the ISO 8601 week-numbering year for a BSON Date. It is the year that owns the ISO week containing the date — which can differ from the calendar year near January and December.
The syntax is { $isoWeekYear: <dateExpression> }. Pass a field reference like "$orderDate", a literal ISODate, or another expression that evaluates to a date.
$year returns the calendar year (e.g. 2023 for any date in 2023). $isoWeekYear returns the ISO week year — for example, 2023-01-01 can be ISO week year 2022 because it falls in week 52 of 2022.
Yes. Always pair them when grouping or filtering by week. Week 1 of ISO year 2024 and week 1 of ISO year 2025 are different buckets even though both are "week 1."
$isoWeekYear 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 $jsonSchema for document validation, or review $isoWeek for week numbers.

Next: $jsonSchema →

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