MongoDB $dateAdd Operator

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

What Is $dateAdd?

$dateAdd is a MongoDB aggregation expression that adds a number of time units (day, month, hour, and more) to a start date and returns the new date. Prefer it over raw millisecond math when you need calendar units.

Remember
$dateAdd = startDate + amount of unit

{
  $dateAdd: {
    startDate: <date>,
    unit: "<unit>",
    amount: <number>,
    timezone: "<tz>"   // optional
  }
}

units: year, quarter, month, week, day,
       hour, minute, second, millisecond

null startDate → null
UsePrefer
Add days / months / hours$dateAdd
Move a date backward$dateSubtract
Add only milliseconds$add (Date + number)

How to Use $dateAdd in MongoDB

Pick a start date, choose a unit, and say how many units to add.

  1. Choose an expression stage ($addFields or $project).
  2. Set startDate to a Date field or expression.
  3. Set unit (for example "day") and amount (for example 7).
  4. Optionally set timezone when calendar boundaries matter for a zone.
PieceRequired?Role
startDateYesDate to start from
unitYesWhat to add (day, month, hour, …)
amountYesHow many units (can be negative)
timezoneNoOlson timezone string when needed

Pseudocode

Pseudocode
for each document:
    start = evaluate(startDate)
    unit  = evaluate(unit)
    n     = evaluate(amount)
    if start is null or missing:
        result = null
    else:
        result = start + n units   // calendar-aware
    emit { ..., field: result }

Cheat Sheet

Keep these facts handy while writing date-add expressions.

TopicDetail
TypeAggregation expression (not a find filter)
Syntax{ $dateAdd: { startDate, unit, amount } }
Common unitsday, month, week, hour
Negative amountMoves backward (like subtract)
vs $add$add uses milliseconds only
Sibling$dateSubtract for going back
Sample check2026-01-10 + 7 days → 2026-01-17

Live Preview

Watch $dateAdd move a borrow date forward by days or months.

Simulates $dateAdd from start date 2026-01-10 (UTC midnight).

Result
startDate = 2026-01-10T00:00:00.000Z
unit = "day", amount = 7
$result → 2026-01-17T00:00:00.000Z

Worked Walkthrough — Library Due Date

A book is borrowed on 2026-01-10. The library allows 7 days. What is the due date?

Calculation
Document:
  { task: "Library book", borrowedOn: 2026-01-10 }

$dateAdd:
  startDate = borrowedOn
  unit      = "day"
  amount    = 7

2026-01-10 + 7 days → 2026-01-17

dueDate = 2026-01-17 ✓

MongoDB Examples

We use one library loan. First add 7 days for the due date, then add 1 month for a renewal date. Run these in mongosh after inserting the sample data once.

Example 1 — Sample Data

Imagine one library loan with a borrow date of 2026-01-10. We save it in a collection named loans.

mongosh
db.loans.insertOne({
  _id: 1,
  task: "Library book",
  borrowedOn: ISODate("2026-01-10T00:00:00Z")
});

How the Data Is Stored in MongoDB

After the insert, MongoDB keeps one document in loans. Run find() to see it:

mongosh
db.loans.find()

Read that document like this:

Field in MongoDBValuePlain English
_id1Document id (row number for this lesson)
task"Library book"What was borrowed
borrowedOn2026-01-10Start date for the loan
Picture
Timeline:

  borrowedOn          due (+7 days)
  2026-01-10  ----->  2026-01-17

Later renewal (+1 month from borrow):
  2026-01-10  ----->  2026-02-10

How It Works

  1. Insert once. insertOne adds a single loan document to loans.
  2. Store a real Date. ISODate(...) keeps borrowedOn as a Date, not a string.
  3. find() shows the same shape. Ready for $dateAdd in the next examples.

Example 2 — Add Days

Goal: compute dueDate as borrow date plus 7 days.

mongosh
db.loans.aggregate([
  {
    $addFields: {
      dueDate: {
        $dateAdd: {
          startDate: "$borrowedOn",
          unit: "day",
          amount: 7
        }
      }
    }
  }
]);

How It Works

  1. Read the start date. MongoDB already has borrowedOn: 2026-01-10 from Example 1.
  2. Add 7 day units. unit: "day" and amount: 7 move the calendar forward one week.
  3. Store the result. $addFields writes dueDate as 2026-01-17.

Example 3 — Add One Month

Same loan, but compute a renewal date one calendar month later.

mongosh
db.loans.aggregate([
  {
    $addFields: {
      renewOn: {
        $dateAdd: {
          startDate: "$borrowedOn",
          unit: "month",
          amount: 1
        }
      }
    }
  }
]);

How It Works

  1. Same stored document. Still using borrowedOn: 2026-01-10 from Example 1.
  2. Use the month unit. unit: "month" adds one calendar month, not a fixed 30 days.
  3. Easy check. January 10 plus one month lands on February 10.

Edge Cases & Pitfalls

Check these before using $dateAdd in production pipelines.

month

Month lengths vary

Adding one month from Jan 31 may land on Feb 28/29. Prefer day when you need an exact day count.

vs add

Do not confuse with $add

$add treats numbers as milliseconds. Use $dateAdd for day/month/hour units.

tz

Timezone can change boundaries

Day and month math can depend on the zone near DST. Set timezone when your app is zone-aware.

null

Null start dates stay null

Missing startDate yields null, not an error. Validate or default dates before adding.

Performance Notes

ApproachCostBest for
$dateAdd per documentCheap date mathDue dates, renewals, SLA windows
Many nested date opsStill O(docs)Multi-step schedules
Filter after computingScan + computePrefer $match on stored dates when possible

Date arithmetic is cheap compared with large sorts or joins. Filter early with $match when only some documents need a computed date.

Key Takeaways

  • Role: add calendar/clock units to a date.
  • Required: startDate, unit, amount.
  • Units: day, month, hour, and more—not just milliseconds.
  • Sibling: use $dateSubtract to go backward.

One line: $dateAdd answers “what date is N units after this?”—with real day/month units, not only milliseconds.

Frequently Asked Questions

$dateAdd adds a number of calendar or clock units to a start date and returns the new date. Use it inside aggregation stages such as $project and $addFields.
Use { $dateAdd: { startDate: <date>, unit: <string>, amount: <number>, timezone: <tz> } }. startDate, unit, and amount are required; timezone is optional.
Supported units include year, quarter, month, week, day, hour, minute, second, and millisecond. Pass the unit as a string such as "day" or "month".
$add can add milliseconds to a Date. $dateAdd understands calendar units like month and day, so month lengths and DST are handled more naturally.
$dateAdd moves a date forward. $dateSubtract moves it backward by the same unit/amount style.
If startDate is null or missing, $dateAdd returns null.

Next: $dateDiff

Measure how many units separate two dates in aggregation.

$dateDiff tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful