$dateSubtract - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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 the date expression.

  • unit: A string that specifies the time unit for the subtrahend expression. 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:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); const events = db.collection('events'); const result = await events.aggregate([ { $project: { eventName: 1, oneYearAgo: { $dateSubtract: { startDate: "$eventTime", amount: 1, unit: "year" } } } } ]).toArray(); console.log(result); await client.close(); } example();
Python
from pymongo import MongoClient from bson.date_time import datetime def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] events = db['events'] result = list(events.aggregate([ { '$project': { 'eventName': 1, 'oneYearAgo': { '$dateSubtract': { 'startDate': '$eventTime', 'amount': 1, 'unit': 'year' } } } } ])) print(result) client.close() example()