MongoDB $isoWeekYear Operator

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

What Is $isoWeekYear?

$isoWeekYear is a MongoDB aggregation expression that returns the ISO 8601 week-numbering year for a date—the year that owns the ISO week containing that date. Near year boundaries it can differ from the calendar $year.

Remember
$isoWeekYear = year that owns the ISO week

{ $isoWeekYear: <dateExpression> }

or

{ $isoWeekYear: { date: <date>, timezone: "<tz>" } }

2025-12-29 → isoWeekYear 2026 (week 1)
             calendar $year → 2025

null / missing → null
always pair with $isoWeek for keys
UsePrefer
ISO week-year for reports$isoWeekYear
ISO week number$isoWeek
Calendar year only$year

How to Use $isoWeekYear in MongoDB

Pass a Date expression and emit the ISO week-year (usually with $isoWeek).

  1. Choose an expression stage ($addFields or $project).
  2. Pass a Date field (for example "$orderDate") to $isoWeekYear.
  3. Also extract $isoWeek when you need a full year-week key.
  4. Optionally set timezone when the local calendar day matters.
PieceRequired?Role
Date expressionYesSource Date to inspect
timezoneNoInterpret the date in a named zone
$isoWeekRecommendedWeek half of the ISO key

Pseudocode

Pseudocode
for each document:
    d = evaluate(dateExpression)
    if d is null or missing:
        result = null
    else:
        week = ISO_8601_week(d)
        result = year that owns that week
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while building ISO year-week keys.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $isoWeekYear: <date> }
vs $yearWeek-year vs calendar year
Boundary tipLate Dec / early Jan may disagree
Pair with$isoWeek
Report key2026-W01 style labels
Sample check2025-12-29 → ISO year 2026

Live Preview

Watch $isoWeekYear (and calendar $year) for dates near the year boundary.

Simulates $isoWeekYear vs calendar year in UTC.

Result
date = 2025-12-29T00:00:00.000Z
$year → 2025
$isoWeekYear → 2026
$isoWeek → 1

{ isoWeekYear: 2026, isoWeek: 1 }

Worked Walkthrough — Year Boundary

An order was placed on Monday, 2025-12-29. That day is still calendar 2025, but it starts ISO week 1 of 2026.

Calculation
Document:
  { order: "Notebook", orderDate: 2025-12-29 }

Calendar year ($year):
  2025

ISO week containing this Monday:
  2025-12-29 … 2026-01-04  → week 1 of 2026

$isoWeekYear → 2026
$isoWeek     → 1

Report key: 2026-W01 ✓

MongoDB Examples

We use one boundary order date. First extract the ISO week-year, then compare it with calendar $year and add $isoWeek. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one order placed on 2025-12-29 (a Monday at the year boundary). We save it in a collection named orders.

mongosh
db.orders.insertOne({
  _id: 1,
  order: "Notebook",
  orderDate: ISODate("2025-12-29T00:00:00Z")
});

How the Data Is Stored in MongoDB

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

mongosh
db.orders.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
order"Notebook"What was ordered
orderDate2025-12-29Boundary date for ISO week-year
Picture
Calendar:     Dec 29, 2025
ISO week 1:   Dec 29, 2025 … Jan 4, 2026
                       |
              owned by ISO year 2026

$year         → 2025
$isoWeekYear  → 2026
$isoWeek      → 1

How It Works

  1. Insert once. insertOne adds a single order document to orders.
  2. Store a real Date. ISODate(...) keeps orderDate as a Date, not a string.
  3. find() shows the same shape. Ready for $isoWeekYear next.

Example 2 — ISO Week-Year

Goal: add isoWeekYear for orderDate.

mongosh
db.orders.aggregate([
  {
    $addFields: {
      isoWeekYear: {
        $isoWeekYear: "$orderDate"
      }
    }
  }
]);

How It Works

  1. Read the stored date. MongoDB already has orderDate: 2025-12-29 from Example 1.
  2. Find the week’s year. That Monday belongs to ISO week 1 of 2026.
  3. Store the year. $addFields writes isoWeekYear: 2026.

Example 3 — Compare with $year

Same order, but also extract calendar $year and $isoWeek so you can see the full picture.

mongosh
db.orders.aggregate([
  {
    $addFields: {
      calendarYear: { $year: "$orderDate" },
      isoWeekYear: { $isoWeekYear: "$orderDate" },
      isoWeek: { $isoWeek: "$orderDate" }
    }
  }
]);

How It Works

  1. Same stored document. Still using orderDate: 2025-12-29 from Example 1.
  2. Calendar vs ISO. $year stays 2025; $isoWeekYear jumps to 2026.
  3. Complete key. With $isoWeek = 1, the report label is 2026-W01.

Edge Cases & Pitfalls

Check these before using $isoWeekYear in production pipelines.

boundary

Do not assume it equals $year

Late December and early January are the danger zone. Always verify week-year near those dates.

pair

Week alone is not unique

“Week 1” repeats every year. Pair with $isoWeek for unique buckets.

tz

Timezone can flip the day

A UTC evening may be the next local morning. Set timezone when reports follow a local calendar.

null

Null dates stay null

Missing or null dates return null. Validate dates before grouping by week-year.

Performance Notes

ApproachCostBest for
$isoWeekYear per documentCheap date extractWeekly keys on the fly
Group by year + weekExtract + groupISO weekly rollups
Store key on writeNo recomputeHot weekly dashboards

Year extraction is cheap. Filter early with $match when only some documents need a week-year.

Key Takeaways

  • Role: year that owns the ISO week.
  • vs $year: can differ near Dec/Jan.
  • Pairing: always use with $isoWeek for keys.
  • Sample: 2025-12-29 → ISO year 2026, week 1.

One line: $isoWeekYear answers “which year owns this date’s ISO week?”—not always the calendar year.

Frequently Asked Questions

$isoWeekYear returns the ISO 8601 week-numbering year for a date—the year that owns the ISO week containing that date. Use it inside aggregation stages such as $project, $addFields, and $group.
Use { $isoWeekYear: <dateExpression> }, or { $isoWeekYear: { date: <dateExpression>, timezone: <tz> } }. Pass a Date field, ISODate literal, or any expression that resolves to a date.
$year is the calendar year printed on the date. $isoWeekYear is the year of the ISO week. Near late December and early January they can disagree—for example 2025-12-29 can be ISO week-year 2026.
Yes. Week 1 of 2025 and week 1 of 2026 are different buckets. Pair both operators for correct year-week keys.
$isoWeekYear returns null when the input is null or the field is missing.
Usually only near year boundaries. If a date’s ISO week belongs mostly to the other calendar year, $isoWeekYear follows the week, not the printed calendar year.

Next: $jsonSchema

Validate documents against a JSON Schema in queries and validation.

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