MongoDB $dateAdd Operator

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 4 Examples
Date Operations

What You’ll Learn

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.

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.

📝 Syntax

The $dateAdd operator takes an object with at least three fields:

mongosh
{
  $dateAdd: {
    startDate: <dateExpression>,
    unit: <string>,
    amount: <numberExpression>,
    timezone: <string>
  }
}

Syntax Rules

  • startDate — the base date (field reference, $$NOW, or literal).
  • unit — one of: year, quarter, week, day, hour, minute, second, millisecond.
  • amount — how many units to add (integer or expression).
  • timezone — optional Olson timezone ID (e.g. "America/New_York").
  • Use inside $project, $addFields, $set, or update aggregation pipelines.
  • Negative amount moves the date backward (prefer $dateSubtract for clarity).

💡 $dateAdd vs $dateSubtract vs $dateDiff

$dateAdd — startDate + N units → new date
$dateSubtract — startDate − N units → new date
$dateDiff — difference between two dates in units (returns a number)

⚡ Quick Reference

QuestionAnswer
Operator typeAggregation expression operator (date)
Required fieldsstartDate, unit, amount
Supported unitsyear, quarter, week, day, hour, minute, second, millisecond
MongoDB version5.0+
Common stages$project, $addFields, $set
Add 7 days
{
  $dateAdd: {
    startDate: "$createdAt",
    unit: "day",
    amount: 7
  }
}

One week later

Add 1 year
{
  $dateAdd: {
    startDate: "$renewalDate",
    unit: "year",
    amount: 1
  }
}

Annual renewal

With timezone
{
  $dateAdd: {
    startDate: "$start",
    unit: "hour",
    amount: 24,
    timezone: "UTC"
  }
}

Timezone-aware add

From now
{
  $dateAdd: {
    startDate: "$$NOW",
    unit: "day",
    amount: 30
  }
}

30 days from pipeline run

Examples Gallery

Calculate trial end dates, subscription renewals, and timezone-aware deadlines with $dateAdd.

📚 Add Days to a Date

Use a subscriptions collection and compute a trial end date from startDate.

Sample Input Documents

Suppose you have a subscriptions collection with a startDate field:

mongosh
[
  {
    "_id": ObjectId("609c26812e9274a86871bc6a"),
    "plan": "Trial",
    "startDate": ISODate("2024-06-01T00:00:00Z")
  },
  {
    "_id": ObjectId("609c26812e9274a86871bc6b"),
    "plan": "Pro",
    "startDate": ISODate("2024-03-15T10:30:00Z")
  }
]

Example 1 — Add 14 Days for Trial End

Compute trialEndDate by adding 14 days to startDate:

mongosh
db.subscriptions.aggregate([
  {
    $project: {
      plan: 1,
      startDate: 1,
      trialEndDate: {
        $dateAdd: {
          startDate: "$startDate",
          unit: "day",
          amount: 14
        }
      }
    }
  }
])

How It Works

  • $project keeps existing fields and adds trialEndDate.
  • unit: "day" and amount: 14 add exactly 14 calendar days.
  • Result is a BSON date ready for comparisons or sorting.

📈 Practical Patterns

Annual renewals, hours for SLAs, and timezone-aware scheduling.

Example 2 — Add One Year for Renewal

Calculate the next renewal date for annual subscriptions:

mongosh
db.subscriptions.aggregate([
  {
    $project: {
      plan: 1,
      startDate: 1,
      nextRenewal: {
        $dateAdd: {
          startDate: "$startDate",
          unit: "year",
          amount: 1
        }
      }
    }
  }
])

// startDate: 2024-03-15 → nextRenewal: 2025-03-15

How It Works

The year unit handles calendar-year arithmetic, including leap years. Use quarter for three-month billing cycles.

Example 3 — Add Hours for SLA Deadline

Add 48 hours to an incident timestamp for a response deadline:

mongosh
db.tickets.aggregate([
  {
    $project: {
      title: 1,
      openedAt: 1,
      slaDeadline: {
        $dateAdd: {
          startDate: "$openedAt",
          unit: "hour",
          amount: 48
        }
      }
    }
  }
])

How It Works

Fine-grained units like hour, minute, and millisecond suit SLAs, session timeouts, and precise scheduling.

Example 4 — Timezone-Aware Date Add

Add one day using a specific timezone so daylight saving is handled correctly:

mongosh
db.events.aggregate([
  {
    $project: {
      title: 1,
      eventStart: 1,
      nextDayLocal: {
        $dateAdd: {
          startDate: "$eventStart",
          unit: "day",
          amount: 1,
          timezone: "America/New_York"
        }
      }
    }
  }
])

How It Works

The optional timezone field uses an Olson timezone identifier. Without it, MongoDB uses UTC. Set timezone when business rules depend on local calendar days.

🚀 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.

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.

💡 Best Practices

✅ Do

  • Store dates as BSON Date type, not strings
  • Use timezone when local calendar rules matter
  • Pick the right unit (day vs hour vs year)
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about $dateAdd

Use these points when adding time to dates in MongoDB.

5
Core concepts
📝 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.

Continue the Operator Series

Move on to $dateDiff to measure time between dates, or review $convert for type conversion.

Next: $dateDiff →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful