$dayOfWeek is a MongoDB aggregation expression that returns the weekday number for a date as an integer from 1 to 7, where 1 is Sunday and 7 is Saturday.
Remember
$dayOfWeek = weekday number from a date
{ $dayOfWeek: <dateExpression> }
{ $dayOfWeek: { date: <date>, timezone: <tz> } }
1 = Sunday
2 = Monday
3 = Tuesday
4 = Wednesday
5 = Thursday
6 = Friday
7 = Saturday
default timezone → UTC
string date → error
null / missing → null
Ensure the argument is a Date / Timestamp / ObjectId (not a plain string).
Pass that expression to $dayOfWeek (add timezone when you need local weekdays).
Optionally filter weekends with $match on values 1 and 7.
Piece
Required?
Role
$dayOfWeek
Yes
Weekday → integer 1–7
Date expression
Yes
Field, ISODate, Timestamp, or ObjectId
timezone
No
Local weekday instead of UTC
Pseudocode
Pseudocode
for each document:
d = evaluate(dateExpression) // Date / Timestamp / ObjectId
if d is null or missing:
result = null
else if d is a string:
error
else:
// optional timezone shifts the calendar day first
result = weekdayNumber(d) // 1=Sun … 7=Sat
emit { ..., field: result }
MongoDB’s weekday scale starts on Sunday. That is different from ISO weekdays (Monday = 1).
Calculation
UTC date: 2024-01-07T12:00:00Z
Calendar weekday → Sunday
$dayOfWeek numbering:
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
$dayOfWeek(...) → 1
Compare with ISO:
$isoDayOfWeek for the same Sunday → 7
Pick the operator that matches your report’s week start.
Code
MongoDB Examples
We store three events on Sunday, Monday, and Saturday, extract the weekday number, then keep only weekend events. Run these in mongosh after inserting the sample data once.
Example 1 — Sample Data
Imagine a tiny events collection with three UTC dates: Sunday, Monday, and Saturday.
Add the weekday first.$addFields computes dayOfWeek for every event.
Keep weekends only.$match with $in: [1, 7] drops Monday.
Trim the output.$project keeps title and dayOfWeek.
Easy check. Kickoff (Sun) and Demo (Sat) remain; Standup (Mon) is gone.
Edge Cases & Pitfalls
Check these before using $dayOfWeek in production pipelines.
numbering
Sunday is 1, not Monday
If your product week starts on Monday, use $isoDayOfWeek instead.
timezone
UTC by default
A late evening local time can be the next UTC day. Pass timezone when the local weekday matters.
types
Strings are not dates
"2024-01-07" as a string errors. Store BSON dates, or convert with $dateFromString first.
null
Null and missing become null
Missing date fields return null, not an error. Filter them before weekend logic if nulls are invalid.
Analysis
Performance Notes
Approach
Cost
Best for
$dayOfWeek alone
Per-document date math
Reports and labels
+ $match on weekday
Still scans after compute
Weekend / weekday filters
Store weekday field
Extra write / storage
Heavy repeated weekday filters
Date extraction is cheap compared with large joins. Filter early with $match on the date range when you only need one week or month.
Remember
Key Takeaways
Role: weekday number from a date → 1–7.
Scale: Sunday = 1, Saturday = 7.
Timezone: UTC by default; pass timezone for local days.
ISO weeks: use $isoDayOfWeek when Monday should be 1.
One line:$dayOfWeek answers “which weekday is this date?”—as 1–7 with Sunday first.
Frequently Asked Questions
$dayOfWeek returns an integer from 1 to 7 for the weekday of a date. In MongoDB, 1 is Sunday, 2 is Monday, through 7 is Saturday. Use it inside aggregation expression stages such as $project and $addFields.
Short form: { $dayOfWeek: <dateExpression> }. Document form: { $dayOfWeek: { date: <dateExpression>, timezone: <tz> } }. The date must resolve to a Date, Timestamp, or ObjectId—not a string.
$dayOfWeek uses Sunday = 1 and Saturday = 7. $isoDayOfWeek uses Monday = 1 and Sunday = 7 (ISO-8601). Pick the numbering your reports expect.
Yes by default. Pass timezone (Olson id like "America/New_York" or an offset like "-0400") in the document form when you need a local weekday.
$dayOfWeek returns null when the input is null or the field is missing. A plain string date causes an aggregation error.
Yes. Project or compute $dayOfWeek, then keep values 1 and 7 (Sunday and Saturday) with $match and $in, or use $expr in $match.