The $hour operator extracts the hour (0–23) from a BSON Date inside MongoDB aggregation pipelines. Use it for peak-traffic analysis, business-hours filtering, hourly dashboards, and time-of-day reporting.
01
Extract Hour
Date → integer 0–23.
02
Syntax
One date argument.
03
Timezone Option
Local hour support.
04
UTC Default
0–23 in UTC.
05
Use Cases
Traffic, shifts.
06
Related Ops
$minute, $dateTrunc.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $hour operator takes a date expression and returns an integer between 0 and 23. For example, $hour applied to ISODate("2024-06-15T14:30:00Z") returns 14, and ISODate("2024-06-15T02:15:00Z") returns 2.
💡
Beginner Tip
$hour uses UTC by default. For business-hours logic in a local timezone, use the object form with a timezone option (see Example 4).
Foundation
📝 Syntax
The $hour operator takes a date expression, or an object with date and timezone:
Grouping by $hour aggregates all days together — useful for “which hour of the day is busiest?” charts. Add $dateTrunc if you need per-day hourly buckets instead.
The object form with timezone converts the instant to the specified zone before extracting the hour. Use this when business rules follow local time, not UTC.
Applications
🚀 Use Cases
Peak traffic analysis — group events by hour to find busiest times of day.
Business hours filtering — select logs or orders during working hours.
Shift scheduling — categorize records into morning, afternoon, or night shifts.
SLA monitoring — flag incidents outside support hours using hour ranges.
🧠 How $hour Works
1
MongoDB resolves the date
The input expression is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$hour extracts the hour
MongoDB reads the hour component as an integer 0–23 (UTC or specified timezone).
Extract
3
An integer is returned
The number is ready for $match, $group, or charts.
Output
=
📅
Hour for time-of-day analytics
Combine with $minute, $dayOfWeek, and $dateTrunc for richer time reporting.
Wrap Up
Conclusion
The $hour operator is a simple way to extract the hour component from BSON dates in MongoDB aggregation pipelines. It is especially useful for peak-traffic charts, business-hours filters, and hourly dashboards.
Remember it uses UTC by default. For local business hours, pass a timezone in the object form. Next in the series: $ifNull.
Use the timezone option for local business-hour logic
Combine with $gte and $lte for hour ranges
Use $expr when filtering by $hour in find()
Prefer BSON dates over strings for reliable extraction
Pair with $dateTrunc when you need hourly date buckets
❌ Don’t
Assume UTC hour matches local business hours
Confuse $hour with $dateTrunc hourly bucketing
Use $hour on string dates — parse first with $dateFromString
Forget that hour 0 is midnight and hour 23 is 11 PM
Expect minutes to affect the result — only the hour is returned
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $hour
Use these points when extracting hours from dates in MongoDB.
5
Core concepts
📅01
Hour Component
0–23 from a date.
Purpose
📝02
One Argument
Date expression.
Syntax
🛠03
Integer Output
Not a Date.
Output
🌐04
Timezone Option
Local hour support.
Note
⚠05
UTC Default
0 = midnight UTC.
Important
❓ Frequently Asked Questions
$hour returns an integer from 0 to 23 representing the hour portion of a BSON Date. For example, 2:30 PM UTC returns 14.
Simple form: { $hour: <dateExpression> }. With timezone: { $hour: { date: <dateExpression>, timezone: <tz> } }. Pass a field like "$loggedAt" or a literal ISODate.
By default, $hour uses UTC. To get the hour in a specific timezone, use the object form with a timezone string like "America/New_York".
$hour returns an integer 0–23 for the hour component. $dateTrunc returns a Date rounded down to a period boundary (hour, day, month) — better for grouping into time buckets.
$hour is available in MongoDB 3.4 and later in aggregation pipelines and update pipelines that use aggregation expressions.