$toDate
New from version 4.0
The $toDate aggregation operator in Amazon DocumentDB is used to convert a date or date and time string to a BSON Date type. This is the inverse operation of the $dateToString operator.
Parameters
-
dateString: A string representation of a date or date and time to be converted to a BSON Date type. -
format: (optional) A string that specifies the format of thedateString. If not provided, the operator will attempt to parse thedateStringin various standard date and time formats. -
timezone: (optional) A string representing the time zone to use for the conversion. If not provided, the local time zone is used.
Example (MongoDB Shell)
The following example demonstrates how to use the $toDate operator to convert a date string to a BSON Date type.
Create sample documents
db.events.insertMany([ { _id: 1, eventName: "Mission Start", eventTime: "2023-04-15T10:30:00Z" }, { _id: 2, eventName: "Checkpoint Reached", eventTime: "2023-04-15T11:15:00Z" }, { _id: 3, eventName: "Mission End", eventTime: "2023-04-15T12:00:00Z" } ]);
Query example
db.events.aggregate([ { $project: { eventName: 1, eventTimeDate: { $toDate: "$eventTime" } } } ]);
Output
[
{
"_id": 1,
"eventName": "Mission Start",
"eventTimeDate": ISODate("2023-04-15T10:30:00Z")
},
{
"_id": 2,
"eventName": "Checkpoint Reached",
"eventTimeDate": ISODate("2023-04-15T11:15:00Z")
},
{
"_id": 3,
"eventName": "Mission End",
"eventTimeDate": ISODate("2023-04-15T12:00:00Z")
}
]
Code examples
To view a code example for using the $toDate command, choose the tab for the language that you want to use: