The $second operator extracts the seconds portion (0–59) from a BSON Date inside MongoDB aggregation pipelines. Use it for log timing analysis, sub-minute patterns, latency bucketing, and precise time breakdowns.
01
Extract Seconds
Date → 0–59.
02
Syntax
One date argument.
03
Timezone Option
Local second support.
04
UTC Default
0–59 in UTC.
05
Use Cases
Logs, bursts, timing.
06
Related Ops
$minute, $millisecond.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $second operator takes a date expression and returns an integer between 0 and 59. For example, $second applied to ISODate("2024-06-15T14:30:45Z") returns 45, and ISODate("2024-06-15T09:05:07Z") returns 7.
Seconds sit between $minute and $millisecond in MongoDB’s date-extraction family. Together with $hour and $minute, they let you break any timestamp into readable parts for filtering, grouping, or display.
💡
Beginner Tip
$second uses UTC by default. For log analysis in local time, use the object form with a timezone option (see Example 4).
Foundation
📝 Syntax
The $second operator takes a date expression, or an object with date and timezone:
The object form with timezone converts the instant to the specified zone before extracting the second. UTC and local values can differ when the local minute boundary does not align with UTC.
Bonus — Full H:M:S Breakdown
Build a readable time-of-day string from hour, minute, and second:
Combine $hour, $minute, and $second for a complete clock-time breakdown. Add $millisecond when you need sub-second precision.
Applications
🚀 Use Cases
Log timing — analyze when within each minute events occur.
Sub-minute bursts — group high-frequency data by second for spike detection.
Cron alignment — find jobs or heartbeats that fire at second 0.
Time breakdowns — combine with $hour and $minute for display or reporting.
🧠 How $second Works
1
MongoDB resolves the date
The expression evaluates to a BSON Date (or null).
Input
2
Timezone is applied if provided
With timezone, the instant is interpreted in that zone before extraction.
Timezone
3
The second component is extracted
MongoDB returns an integer from 0 to 59 representing seconds within the minute.
Extract
=
⏱
Integer 0–59
Use in projections, filters, or group keys for time-based analysis.
Wrap Up
Conclusion
The $second operator extracts the seconds portion (0–59) from a BSON Date in MongoDB aggregation pipelines. It is a building block for log analysis, sub-minute grouping, and precise time breakdowns alongside $hour and $minute.
Remember: UTC by default, optional timezone for local time, and null when the date is missing. Next in the series: $setDifference.
Use timezone when reporting in local business hours
Combine with $hour and $minute for full clock breakdowns
Use $millisecond when sub-second precision matters
Handle null dates with $ifNull in downstream logic
Use $dateTrunc for time bucketing instead of many components
❌ Don’t
Assume UTC seconds match local seconds without checking timezone
Use $second on string dates — convert with $toDate first
Forget that grouping by second alone merges all hours and days
Confuse $second with elapsed duration (use date arithmetic instead)
Expect fractional seconds — use $millisecond for that
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $second
Use these points when extracting seconds from dates in MongoDB.
5
Core concepts
⏱01
0–59
Seconds in minute.
Purpose
📝02
One date arg
Field or ISODate.
Syntax
🛠03
UTC default
timezone optional.
Timezone
🔄04
Date family
$minute, $hour.
Related
📑05
Log analysis
Bursts & timing.
Use case
❓ Frequently Asked Questions
$second returns an integer from 0 to 59 representing the seconds portion of a BSON Date. For example, 14:30:45 UTC returns 45, and 9:05:07 UTC returns 7.
Simple form: { $second: <dateExpression> }. With timezone: { $second: { date: <dateExpression>, timezone: <tz> } }. Pass a field like "$loggedAt" or a literal ISODate.
By default, $second uses UTC. To get the second in a specific timezone, use the object form with a timezone string like "America/Los_Angeles".
$second returns 0–59 (seconds within the minute). $minute returns 0–59 (minutes within the hour). $millisecond returns 0–999 (fractional part of the second).
$second is available in MongoDB 3.4 and later in aggregation pipelines and update pipelines that use aggregation expressions.