$dayOfWeek (Get Day of Week)

Last updated: 2026-10-09

$dayOfWeek (Get Day of Week)

$dayOfWeek returns the weekday of a date as an integer: 1 is Sunday, 7 is Saturday — matching JavaScript's Date.getDay(). Often used for weekday-based traffic stats or scheduling. Note numbering starts on Sunday, unlike ISO $isoDayOfWeek (Monday is 1), so confirm the semantics you need. timezone drives the weekday boundary and defaults to UTC.

CategoryDate
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

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

⚙️ Parameters

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

Returns:Weekday number (1=Sunday, 7=Saturday); null if date is null.

▶ Example

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

// $dayOfWeek: count visits by weekday (Beijing time)
 db.logs.aggregate([
  { $group: {
      _id: { $dayOfWeek: { date: '$ts', timezone: 'Asia/Shanghai' } },
      cnt: { $sum: 1 }
  } }
])

Output:

{ "_id": 1, "cnt": 1240 }
{ "_id": 2, "cnt": 980 }

📌 Notes

More hands-on cases coming soon.

📚 Related Tutorials

❓ FAQ

QWhat day is 1?
A1 is Sunday, 7 is Saturday — same as JavaScript's getDay.
QHow to get Monday-first numbering?
AUse $isoDayOfWeek: 1 is Monday, 7 is Sunday.
QCan I get the weekday name directly?
ANo — convert via $switch or the %A placeholder of $dateToString.
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%

🙏 帮我们做得更好

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

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