$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%Yfor the four-digit year,%mfor the two-digit month,%dfor 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 thedateparameter isnull.
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: