$dateToString - 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).

$dateToString

The $dateToString aggregation operator in Amazon DocumentDB is used to convert a date or timestamp value to a string representation. This is useful when you need to format the date and time in a specific way for display or further processing.

Parameters

  • date: The date or timestamp value to be converted to a string.

  • format: A string that specifies the format in which the date should be represented. The format string can include various format specifiers, such as %Y for the four-digit year, %m for the two-digit month, %d for the two-digit day of the month, etc.

  • timezone: (optional) The time zone to use for the conversion. If not specified, the time zone of the server hosting the Amazon DocumentDB cluster is used.

  • onNull: (optional) The value to be returned if the date parameter is null.

Example (MongoDB Shell)

The following example demonstrates the usage of the $dateToString operator to format the logDate field of the missionLog collection.

Create sample documents

db.missionLog.insertMany([ { _id: 1, "event":"missionStart", logDate: new Date("2020-03-15T13:41:33Z") }, { _id: 2, "event":"jumpPoint1", logDate: new Date("2020-03-15T13:45:34Z") }, { _id: 3, "event":"jumpPoint2", logDate: new Date("2020-03-15T13:48:21Z") }, { _id: 4, "event":"jumpPoint3", logDate: new Date("2020-03-15T13:52:09Z") }, { _id: 5, "event":"missionEnd", logDate: new Date("2020-03-15T13:58:44Z") } ]);

Query example

db.missionLog.aggregate([ { $project: { event: "$event", logDateFormatted: { $dateToString: { format: "%Y-%m-%d %H:%M:%S", date: "$logDate" } } } } ])

Output

[ { "_id": 1, "event": "missionStart", "logDateFormatted": "2020-03-15 13:41:33" }, { "_id": 2, "event": "jumpPoint1", "logDateFormatted": "2020-03-15 13:45:34" }, { "_id": 3, "event": "jumpPoint2", "logDateFormatted": "2020-03-15 13:48:21" }, { "_id": 4, "event": "jumpPoint3", "logDateFormatted": "2020-03-15 13:52:09" }, { "_id": 5, "event": "missionEnd", "logDateFormatted": "2020-03-15 13:58:44" } ]

Code examples

To view a code example for using the $dateToString command, choose the tab for the language that you want to use:

Node.js

Here's an example of using the $dateToString operator in a Node.js application:

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 collection = db.collection('missionLog'); const result = await collection.aggregate([ { $project: { event: "$event", logDateFormatted: { $dateToString: { format: "%Y-%m-%d %H:%M:%S", date: "$logDate" } } } } ]).toArray(); console.log(result); await client.close(); } example();
Python

Here's an example of using the $dateToString operator in a Python application:

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'] collection = db['missionLog'] pipeline = [ { '$project': { 'event': '$event', 'logDateFormatted': { '$dateToString': { 'format': '%Y-%m-%d %H:%M:%S', 'date': '$logDate' } } } } ] result = list(collection.aggregate(pipeline)) print(result) client.close() example()