$dateSubtract
New from version 5.0
The $dateSubtract aggregation operator in Amazon DocumentDB allows you to subtract a specified duration from a date value.
Parameters
-
date: A date expression that resolves to a date or timestamp. -
subtrahend: A duration expression that specifies the amount of time to subtract from thedateexpression. -
unit: A string that specifies the time unit for thesubtrahendexpression. Supported units are "year", "quarter", "month", "week", "day", "hour", "minute", "second", and "millisecond".
Example (MongoDB Shell)
The following example demonstrates how to use the $dateSubtract operator to calculate the date one year ago from the current date.
Create sample documents
db.events.insertOne({ eventName: "Player joined", eventTime: ISODate("2023-04-01T12:00:00Z") });
Query example
db.events.aggregate([ { $project: { eventName: 1, oneYearAgo: { $dateSubtract: { startDate: "$eventTime", amount: 1, unit: "year" } } } } ])
Output
{
"_id" : ObjectId("64567890abcdef012345678"),
"eventName" : "Player joined",
"oneYearAgo" : ISODate("2022-04-01T12:00:00Z")
}
Code examples
To view a code example for using the $dateSubtract command, choose the tab for the language that you want to use: