The $dateAdd operator adds time units to a date inside MongoDB aggregation pipelines. Use it to calculate due dates, subscription renewals, trial expirations, or any future timestamp from a stored date.
01
Date Arithmetic
Add units to dates.
02
Syntax
startDate, unit, amount.
03
Time Units
day, hour, year, etc.
04
Timezone
Optional IANA zone.
05
Use Cases
Due dates, renewals.
06
vs $dateSubtract
Add vs subtract.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateAdd operator increments a startDate by a given amount of unit values and returns the resulting BSON date. For example, adding 7 days to a subscription start date gives you the end of a one-week trial.
💡
Beginner Tip
$dateAdd requires MongoDB 5.0+. The startDate must be a valid date (or date expression). Use $convert first if your field is stored as a string.
Foundation
📝 Syntax
The $dateAdd operator takes an object with at least three fields:
The optional timezone field uses an Olson timezone identifier. Without it, MongoDB uses UTC. Set timezone when business rules depend on local calendar days.
Applications
🚀 Use Cases
Trial and subscription periods — compute end dates from signup or renewal timestamps.
SLA and deadline tracking — add hours or days to incident or task creation times.
Reporting windows — derive period end dates for dashboards and scheduled reports.
Data migration — shift historical dates forward during test or staging transforms.
🧠 How $dateAdd Works
1
MongoDB resolves startDate
The base date is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dateAdd adds the units
MongoDB adds amount of unit, optionally respecting timezone.
Add
3
The new date is stored
The result is written to the field you define in $project or $addFields.
Output
=
📅
Future (or adjusted) date
Ready for $match filters, sorting, or display in reports.
Wrap Up
Conclusion
The $dateAdd operator is the standard way to perform forward date arithmetic in MongoDB aggregation pipelines. With clear unit and amount fields, it replaces manual millisecond math for most business date calculations.
Pair it with $dateSubtract for backward shifts and $dateDiff when you need the distance between two dates. Always confirm your MongoDB version is 5.0+ and that startDate fields are proper BSON dates.
Use $dateSubtract for backward moves (clearer intent)
Test edge cases around leap years and DST boundaries
❌ Don’t
Use $dateAdd on MongoDB versions before 5.0
Pass invalid unit strings (no month unit exists)
Forget to convert string dates with $convert first
Confuse $dateAdd with $dateDiff
Assume negative amount is as readable as $dateSubtract
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateAdd
Use these points when adding time to dates in MongoDB.
5
Core concepts
📅01
Add Time
Forward date math.
Purpose
📝02
Three Fields
startDate, unit, amount.
Syntax
🛠03
8 Units
day, hour, year, etc.
Units
🌐04
Timezone
Optional Olson ID.
Option
⚠05
MongoDB 5.0+
Version requirement.
Important
❓ Frequently Asked Questions
$dateAdd adds a specified number of time units to a date and returns the new date. Use it to compute due dates, renewals, or expiry timestamps inside aggregation pipelines.
{ $dateAdd: { startDate: <date>, unit: <string>, amount: <number>, timezone: <string> } }. Only startDate, unit, and amount are required.
$dateAdd supports year, quarter, week, day, hour, minute, second, and millisecond. Pass the unit as a string like "day" or "hour". There is no "month" unit — add days or use quarter for approximate periods.
$dateAdd moves a date forward in time. $dateSubtract moves it backward by subtracting units from startDate.
$dateAdd is available in MongoDB 5.0 and later, in aggregation pipelines and in update pipelines that use aggregation expressions.