$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.
| Category | Date |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $dayOfWeek: { date: <date-expression>, timezone: <timezone> } }
⚙️ Parameters
| date | Required; the date expression. |
|---|---|
| timezone | Optional 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
- 1 is Sunday, not Monday — a common pitfall; use $isoDayOfWeek for Monday-first
- Combine with $group for weekday-based stats
- Defaults to UTC; pass Asia/Shanghai for China-based business
🏷️ Related Tags
💡 Related Cases
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.