$dateFromString
The $dateFromString aggregation operator in Amazon DocumentDB allows you to convert a date-time string into a date object. This is useful when your data is stored as strings but you need to perform date-based operations on the data.
Parameters
-
dateString: A string that represents a date and time. -
format: (optional) A string that specifies the format of thedateString. If not provided, Amazon DocumentDB will attempt to parse the string in the ISO-8601 format. -
timezone: (optional) A string that specifies the time zone. If not provided, Amazon DocumentDB will use the time zone of the server. -
onError: (optional) Specifies the action to take if the conversion fails. Possible values are'error'(the default, which throws an error),'null'(returnsnull), or'replace'(replaces the value with the replacement string specified in theonErrorMessageoption). -
onErrorMessage: (optional) IfonErroris set to'replace', this option specifies the replacement string.
Example (MongoDB Shell)
The following example demonstrates how to use $dateFromString to convert a date string to a date object in Amazon DocumentDB.
Create sample documents
db.missionLog.insertMany([ { _id: 1, event: "missionStart", logDate: "2020-03-15T13:41:33"}, { _id: 2, event: "jumpPoint1", logDate: "2020-03-15T13:45:34"}, { _id: 3, event: "jumpPoint2", logDate: "2020-03-15T13:48:21"}, { _id: 4, event: "jumpPoint3", logDate: "2020-03-15T13:52:09"}, { _id: 5, event: "missionEnd", logDate: "2020-03-15T13:58:44"} ]);
Query example
db.missionLog.aggregate([ { $project: { event: '$event', logDate: { $dateFromString: { dateString: '$logDate' } } } } ]);
Output
[
{
"_id": 1,
"event": "missionStart",
"logDate": ISODate("2020-03-15T13:41:33Z")
},
{
"_id": 2,
"event": "jumpPoint1",
"logDate": ISODate("2020-03-15T13:45:34Z")
},
{
"_id": 3,
"event": "jumpPoint2",
"logDate": ISODate("2020-03-15T13:48:21Z")
},
{
"_id": 4,
"event": "jumpPoint3",
"logDate": ISODate("2020-03-15T13:52:09Z")
},
{
"_id": 5,
"event": "missionEnd",
"logDate": ISODate("2020-03-15T13:58:44Z")
}
]
Code examples
To view a code example for using the $dateFromString command, choose the tab for the language that you want to use: