MongoDB $isoWeek Operator

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

What Is $isoWeek?

$isoWeek is a MongoDB aggregation expression that returns the ISO 8601 week number (1–53) for a date. ISO weeks start on Monday. Week 1 is the week that contains the year’s first Thursday.

Remember
$isoWeek = ISO week number (1…53)

{ $isoWeek: <dateExpression> }

or

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

weeks start Monday
week 1 contains first Thursday of the year

null / missing → null
pair with $isoWeekYear for reports
UsePrefer
ISO week number (Mon–Sun)$isoWeek
ISO week-year$isoWeekYear
Sunday-based week number$week

How to Use $isoWeek in MongoDB

Pass a Date expression and emit the week number (optionally with the ISO year).

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

Pseudocode

Pseudocode
for each document:
    d = evaluate(dateExpression)
    if d is null or missing:
        result = null
    else:
        result = ISO_8601_week_number(d)   // 1..53
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while extracting ISO weeks.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $isoWeek: <date> }
Range1 to 53
Week startMonday
Week 1 ruleContains first Thursday / Jan 4
vs $week$week is Sunday-based
Sample check2026-01-12 (Mon) → ISO week 3

Live Preview

Watch $isoWeek turn a date into an ISO week number.

Simulates { $isoWeek: date } in UTC.

Result
date = 2026-01-12T00:00:00.000Z
$isoWeek → 3

{ isoWeek: 3 }

Worked Walkthrough — Order Week

An order was placed on Monday, 2026-01-12. Which ISO week is that?

Calculation
Document:
  { order: "Notebook", orderDate: 2026-01-12 }

ISO week rules:
  weeks start Monday
  2026 week 1 starts 2025-12-29
  week 2: 2026-01-05 … 2026-01-11
  week 3: 2026-01-12 … 2026-01-18

$isoWeek(2026-01-12) → 3 ✓

MongoDB Examples

We use one order date. First extract the ISO week, then also extract the ISO week-year for a complete week key. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one order placed on 2026-01-12 (a Monday). We save it in a collection named orders.

mongosh
db.orders.insertOne({
  _id: 1,
  order: "Notebook",
  orderDate: ISODate("2026-01-12T00: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
orderDate2026-01-12Date we will convert to an ISO week
Picture
ISO weeks near early 2026:

  Week 2: Jan 5 … Jan 11
  Week 3: Jan 12 … Jan 18  ← our orderDate
                 |
            $isoWeek → 3

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 $isoWeek next.

Example 2 — ISO Week Number

Goal: add isoWeek as the ISO week number of orderDate.

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

How It Works

  1. Read the stored date. MongoDB already has orderDate: 2026-01-12 from Example 1.
  2. Apply ISO rules. That Monday falls in ISO week 3 of 2026.
  3. Store the number. $addFields writes isoWeek: 3.

Example 3 — With $isoWeekYear

Same order, but also extract the ISO week-year so weekly reports have a full key.

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

How It Works

  1. Same stored document. Still using orderDate: 2026-01-12 from Example 1.
  2. Week + year together. $isoWeekYear supplies the year half of the ISO week key.
  3. Easy report label. You can group as 2026-W03 without mixing calendar-year edge cases.

Edge Cases & Pitfalls

Check these before using $isoWeek in production pipelines.

year

Do not pair with calendar $year alone

Early January / late December can belong to a different ISO week-year. Use $isoWeekYear.

vs week

$week is not the same

$week uses Sunday-based numbering. Pick one convention and stay consistent.

tz

Timezone can change 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, not an error. Validate dates before grouping by week.

Performance Notes

ApproachCostBest for
$isoWeek per documentCheap date extractWeekly labels on the fly
Group by weekExtract + groupWeekly rollups
Store week on writeNo recomputeHot weekly dashboards

Week extraction is cheap. Filter early with $match when only some documents need a week number.

Key Takeaways

  • Role: ISO 8601 week number (1–53).
  • Start day: Monday (not Sunday).
  • Pairing: use $isoWeekYear for report keys.
  • Sibling: $week is a different convention.

One line: $isoWeek answers “which ISO week is this date in?”—Monday-start weeks, numbered 1–53.

Frequently Asked Questions

$isoWeek returns the ISO 8601 week number (1–53) for a date. Use it inside aggregation stages such as $project, $addFields, and $group.
Use { $isoWeek: <dateExpression> }, or { $isoWeek: { date: <dateExpression>, timezone: <tz> } }. Pass a Date field, ISODate literal, or any expression that resolves to a date.
ISO weeks start on Monday. Week 1 is the week that contains the year’s first Thursday (also the week containing January 4). Near year boundaries, a date may belong to week 52/53 of the previous ISO year or week 1 of the next.
Yes for reporting keys. Calendar year and ISO week-year can differ in late December / early January. Use both for a correct year-week label.
$isoWeek follows ISO 8601 (Monday start). $week uses a Sunday-based week numbering that can differ for the same date.
$isoWeek returns null when the input is null or the field is missing.

Next: $isoWeekYear

Get the ISO week-year that pairs correctly with $isoWeek.

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