$dateAdd
New from version 5.0
The $dateAdd aggregation operator in Amazon DocumentDB allows you to add a duration to a date and time value.
Parameters
-
date: A date and time value to add a duration to. -
duration: The duration to add to thedatevalue. This can be specified as an object with keys foryears,months,weeks,days,hours,minutes, andseconds. -
timezone: (optional) The time zone to use when performing the date addition. If not specified, the default time zone of the Amazon DocumentDB cluster is used.
Example (MongoDB Shell)
The following example demonstrates how to use the $dateAdd operator to add 2 days and 12 hours to a date.
Create sample documents
db.events.insertMany([ { _id: 1, eventDate: ISODate("2023-04-01T10:00:00Z") }, { _id: 2, eventDate: ISODate("2023-04-02T12:00:00Z") }, { _id: 3, eventDate: ISODate("2023-04-03T14:00:00Z") } ]);
Query example
db.events.aggregate([ { $project: { _id: 1, eventDate: 1, eventDatePlustwodaysandtwelvehours: { $dateAdd: { startDate: { $dateAdd: { startDate: "$eventDate", unit: "day", amount: 2 } }, unit: "hour", amount: 12 } } } } ])
Output
[
{
"_id": 1,
"eventDate": "2023-04-01T10:00:00Z",
"eventDatePlustwodaysandtwelvehours": ISODate("2023-04-03T22:00:00Z)"
},
{
"_id": 2,
"eventDate": "2023-04-02T12:00:00Z",
"eventDatePlustwodaysandtwelvehours": ISODate("2023-04-05T00:00:00Z)"
},
{
"_id": 3,
"eventDate": "2023-04-03T14:00:00Z",
"eventDatePlustwodaysandtwelvehours": ISODate("2023-04-06T02:00:00Z)"
}
]
Code examples
To view a code example for using the $dateAdd command, choose the tab for the language that you want to use: