$isoWeek (ISO Week Number)

Last updated: 2026-10-09

$isoWeek (ISO Week Number)

$isoWeek returns the ISO 8601 week number of a date (1 to 53). ISO weeks start on Monday, and week 1 is the week containing the first Thursday — unlike the US-style $week (Sunday start, may return 0). Great for international week-based stats; usually paired with $isoWeekYear to form a year-week key. timezone drives the week boundary.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $isoWeek: { date: <date-expression>, timezone: <timezone> } }

⚙️ Parameters

dateRequired; the date expression.
timezoneOptional timezone; defaults to UTC.

Returns:ISO week number (1-53); null if date is null.

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $isoWeek: aggregate sales by ISO week
 db.sales.aggregate([
  { $group: {
      _id: { $isoWeek: { date: '$soldAt', timezone: 'Asia/Shanghai' } },
      total: { $sum: '$amount' }
  } }
])

Output:

{ "_id": 39, "total": 9800 }
{ "_id": 40, "total": 10450 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat is the return range?
A1 to 53 — never 0.
QWhat differs from $week?
A$week starts Sunday and may return 0; $isoWeek follows ISO 8601 starting Monday.
QHow do weeks spanning years count?
AISO assigns them to the year containing the Thursday; use $isoWeekYear for the right year.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏