$year (Get Year)
Last updated: 2026-10-09
$year (Get Year)
$year returns the full year of a date (e.g. 2026) — the most basic date extractor. It often combines with $month or $week into multi-level group keys, or serves yearly archiving and year-over-year analysis. Returns an integer. Note the difference from $isoWeekYear, which returns the ISO week's year and can differ in straddling weeks — prefer it for week-based stats.
| Category | Date |
|---|---|
| MongoDB Version | 1.0+ |
| Example Language | mongosh (Shell) |
📝 Syntax
{ $year: { date: <date-expression>, timezone: <timezone> } }
⚙️ Parameters
| date | Required; the date expression. |
|---|---|
| timezone | Optional timezone; defaults to UTC. |
Returns:The year as an integer; null if date is null.
▶ Example
Run the commands below in your local mongosh (requires a running mongod):
// $year: order totals by year
db.orders.aggregate([
{ $group: {
_id: { $year: { date: '$orderDate' } },
total: { $sum: '$amount' }
} }
])
Output:
{ "_id": 2026, "total": 892000 }
{ "_id": 2025, "total": 764000 }
📌 Notes
- Returns the full four-digit year
- Records with null date land in a null bucket when grouping
- Use $isoWeekYear, not $year, for straddling-week stats
🏷️ Related Tags
💡 Related Cases
More hands-on cases coming soon.
📚 Related Tutorials
❓ FAQ
QHow many digits is the year?
AFour digits, e.g. 2026.
QWhen does it differ from $isoWeekYear?
AIn ISO straddling weeks around New Year, the years can differ by 1.
QCan it be used for yearly archiving?
AYes — filter or group by $year with $match or $group.