The $dateSubtract operator subtracts time units from a date inside MongoDB aggregation pipelines. Use it to compute rolling cutoffs, grace-period starts, retention windows, or any past timestamp relative to a stored date or $$NOW.
01
Date Arithmetic
Subtract units from dates.
02
Syntax
startDate, unit, amount.
03
Time Units
day, hour, year, etc.
04
Timezone
Optional IANA zone.
05
Use Cases
Cutoffs, grace periods.
06
vs $dateAdd
Subtract vs add.
Fundamentals
Definition and Usage
In MongoDB’s aggregation framework, the $dateSubtract operator decrements a startDate by a given amount of unit values and returns the resulting BSON date. For example, subtracting 30 days from $$NOW gives you the cutoff for “active in the last 30 days” queries.
💡
Beginner Tip
$dateSubtract returns a Date, not a number. It mirrors $dateAdd but moves backward in time. Requires MongoDB 5.0+ and a valid BSON date for startDate.
Foundation
📝 Syntax
The $dateSubtract operator takes an object with at least three fields:
The year unit handles calendar-year arithmetic, including leap-year edge cases (Feb 29 maps appropriately). Use the result to join or compare against historical data.
Example 4 — Filter Users Active in the Last 7 Days
Combine $dateSubtract with $match to find recently active users:
MongoDB 5.0+ allows aggregation expressions inside $match when using the aggregation pipeline form of find or in $match after $addFields. Compute the cutoff once, then keep documents where lastLogin is on or after that date.
Applications
🚀 Use Cases
Payment reminders — subtract days from due dates to schedule notifications.
Retention windows — define “active in last N days” cutoffs from $$NOW.
Year-over-year analysis — subtract years from report dates for historical comparison.
Data archival — identify records older than a threshold by subtracting from current time.
🧠 How $dateSubtract Works
1
MongoDB resolves startDate
The base date is evaluated per document — a field, $$NOW, or literal ISODate.
Input
2
$dateSubtract removes the units
MongoDB subtracts amount of unit, optionally respecting timezone.
Subtract
3
The new date is returned
The earlier timestamp is stored in the field you define — ready for filtering or display.
Output
=
📅
A date in the past
Use in $match range queries, reminders, YoY joins, or alongside $dateAdd for bidirectional date math.
Wrap Up
Conclusion
The $dateSubtract operator is the clearest way to perform backward date arithmetic in MongoDB aggregation pipelines. With explicit unit and amount fields, it replaces negative $dateAdd calls and manual millisecond subtraction for most business rules.
Pair it with $dateAdd for forward shifts and $dateDiff when you need the distance between two dates as a number. Always confirm your MongoDB version is 5.0+ and that startDate fields are proper BSON dates.
Use $dateSubtract instead of negative $dateAdd for clarity
Store dates as BSON Date type, not strings
Use timezone when local calendar rules matter
Pick the right unit (day vs hour vs year)
Test edge cases around leap years and DST boundaries
❌ Don’t
Use $dateSubtract on MongoDB versions before 5.0
Pass invalid unit strings (no month unit exists)
Confuse $dateSubtract with $dateDiff
Forget to convert string dates with $dateFromString first
Assume subtracting days always equals 24 × N hours (calendar vs fixed duration)
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $dateSubtract
Use these points when subtracting time from dates in MongoDB.
5
Core concepts
📅01
Subtract Time
Backward date math.
Purpose
📝02
Three Fields
startDate, unit, amount.
Syntax
🛠03
Date Output
Not a number.
Output
🌐04
Timezone
Optional Olson ID.
Option
⚠05
Mirror of $dateAdd
Subtract vs add.
Important
❓ Frequently Asked Questions
$dateSubtract subtracts a specified number of time units from a date and returns the new date. Use it to compute rolling cutoffs, grace-period starts, or past deadlines inside aggregation pipelines.
{ $dateSubtract: { startDate: <date>, unit: <string>, amount: <number>, timezone: <string> } }. startDate, unit, and amount are required — the same shape as $dateAdd, but units are subtracted instead of added.
year, quarter, week, day, hour, minute, second, and millisecond. Pass the unit as a string such as "day" or "hour". There is no "month" unit.
$dateSubtract moves a date backward in time by subtracting units from startDate. $dateAdd moves it forward. Both return a new BSON Date, not a number.
$dateSubtract is available in MongoDB 5.0 and later in aggregation pipelines and update pipelines that use aggregation expressions.