$week (Get Week Number)

Last updated: 2026-10-09

$week (Get Week Number)

$week returns the US-style week number of a date (0 to 53): weeks start on Sunday, week 1 begins with the year's first Sunday, and earlier dates fall in week 0. It differs from the ISO-standard $isoWeek; prefer $isoWeek for international stats. It returns an integer; timezone drives the week boundary and defaults to UTC.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

Returns:The week number (0-53); null if date is null.

▶ Example

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

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

Output:

{ "_id": 38, "total": 8300 }
{ "_id": 39, "total": 9800 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhy does 0 appear?
ADates between Jan 1 and the first Sunday fall in week 0.
QWhich suits international business, $week or $isoWeek?
AUse $isoWeek for international business; $week is the US convention.
QWhat is the maximum?
A0 to 53, unlike $isoWeek's 1 to 53.
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%

🙏 帮我们做得更好

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

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