Skip to main content

$addYears

The $addYears operator adds a specified number of years to a date object.

Input

  • An array containing a date object and an integer number of years to add

Output

  • A new date object with the specified number of years added

Examples

Add Years to Date

{
  "$addYears": [
    {
      "$toDate": ["2023-01-15T10:30:00Z"]
    },
    2
  ]
}
Result: Date object representing January 15, 2025 at 10:30:00 UTC

Add Years to Field Value

{
  "$addYears": [
    {
      "$input": "start_date"
    },
    {
      "$input": "years_to_add"
    }
  ]
}
Result: A new date with years_to_add added to the start_date field