$dateDiff - Amazon DocumentDB

$dateDiff

New from version 5.0

Not supported by Elastic cluster.

The $dateDiff aggregation operator calculates the difference between two dates in specified units. It returns the number of unit boundaries crossed between the start and end dates.

Parameters

  • startDate: The beginning date expression.

  • endDate: The ending date expression.

  • unit: The time unit for the difference. Supported units are year, quarter, month, week, day, hour, minute, second, and millisecond.

Example (MongoDB Shell)

The following example demonstrates how to use the $dateDiff operator to calculate the number of days between order placement and delivery.

Create sample documents

db.shipments.insertMany([ { orderId: 1001, orderDate: ISODate("2025-01-10T08:00:00Z"), deliveryDate: ISODate("2025-01-15T14:30:00Z") }, { orderId: 1002, orderDate: ISODate("2025-02-05T10:00:00Z"), deliveryDate: ISODate("2025-02-12T16:45:00Z") } ]);

Query example

db.shipments.aggregate([ { $project: { orderId: 1, orderDate: 1, deliveryDate: 1, daysToDeliver: { $dateDiff: { startDate: "$orderDate", endDate: "$deliveryDate", unit: "day" } } } } ]);

Output

[ { _id: ObjectId('6924a5f2d66dcae121d29517'), orderId: 1001, orderDate: ISODate('2025-01-10T08:00:00.000Z'), deliveryDate: ISODate('2025-01-15T14:30:00.000Z'), daysToDeliver: 5 }, { _id: ObjectId('6924a5f2d66dcae121d29518'), orderId: 1002, orderDate: ISODate('2025-02-05T10:00:00.000Z'), deliveryDate: ISODate('2025-02-12T16:45:00.000Z'), daysToDeliver: 7 } ]

Code examples

To view a code example for using the $dateDiff 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 shipments = db.collection('shipments'); const result = await shipments.aggregate([ { $project: { orderId: 1, orderDate: 1, deliveryDate: 1, daysToDeliver: { $dateDiff: { startDate: "$orderDate", endDate: "$deliveryDate", unit: "day" } } } } ]).toArray(); console.log(result); client.close(); } example();
Python
from pymongo import MongoClient 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'] shipments = db['shipments'] result = list(shipments.aggregate([ { "$project": { "orderId": 1, "orderDate": 1, "deliveryDate": 1, "daysToDeliver": { "$dateDiff": { "startDate": "$orderDate", "endDate": "$deliveryDate", "unit": "day" } } } } ])) print(result) client.close() example()