Is there a way to convert string to date using custom format using mongodb shell
I am trying to convert "21/May/2012:16:35:33 -0400" to date,
Is there a way to pass DateFormatter
or something to
Date.parse(...)
or ISODate(....)
method?
Is there a way to convert string to date using custom format using mongodb shell
I am trying to convert "21/May/2012:16:35:33 -0400" to date,
Is there a way to pass DateFormatter
or something to
Date.parse(...)
or ISODate(....)
method?
You can use
$dateFromString
aggregation which converts string date to ISO dateI had some strings in the MongoDB Stored wich had to be reformated to a proper and valid dateTime field in the mongodb.
here is my code for the special date format: "2014-03-12T09:14:19.5303017+01:00"
but you can easyly take this idea and write your own regex to parse the date formats:
How about using a library like momentjs by writing a script like this:
Then load the script at the command line like so:
Finally, in your next mongo session, use it like so:
You can use the javascript in the second link provided by Ravi Khakhkhar or you are going to have to perform some string manipulation to convert your orginal string (as some of the special characters in your original format aren't being recognised as valid delimeters) but once you do that, you can use "new"
Here's some links that you may find useful (regarding modification of the data within the mongo shell) -
http://cookbook.mongodb.org/patterns/date_range/
http://www.mongodb.org/display/DOCS/Dates
http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell
Using MongoDB 4.0 and newer
The
$toDate
operator will convert the value to a date. If the value cannot be converted to a date,$toDate
errors. If the value is null or missing,$toDate
returns null:You can use it within an aggregate pipeline as follows:
The above is equivalent to using the
$convert
operator as follows:Using MongoDB 3.6 and newer
You cab also use the
$dateFromString
operator which converts the date/time string to a date object and has options for specifying the date format as well as the timezone:Using MongoDB versions
>= 2.6 and < 3.2
If MongoDB version does not have the native operators that do the conversion, you would need to manually iterate the cursor returned by the
find()
method by either using theforEach()
method or the cursor methodnext()
to access the documents. Withing the loop, convert the field to an ISODate object and then update the field using the$set
operator, as in the following example where the field is calledcreated_at
and currently holds the date in string format:For improved performance especially when dealing with large collections, take advantage of using the Bulk API for bulk updates as you will be sending the operations to the server in batches of say 1000 which gives you a better performance as you are not sending every request to the server, just once in every 1000 requests.
The following demonstrates this approach, the first example uses the Bulk API available in MongoDB versions
>= 2.6 and < 3.2
. It updates all the documents in the collection by changing thecreated_at
fields to date fields:Using MongoDB 3.2
The next example applies to the new MongoDB version
3.2
which has since deprecated the Bulk API and provided a newer set of apis usingbulkWrite()
:In my case I have succeed with the following solution for converting field ClockInTime from ClockTime collection from string to Date type: