just practicing my MongoDB queries and I've hit a wall with a field data type.
I'm currently using Robomongo as GUI for accessing the production database.
My document structure looks like:
Is there a MongoDB operator or way/method to convert the date
field value, currently in mm/dd/yyyy
format, to a Unix timestamp so we can perform filter operations?
You can iterate all your items and update one by one with the conversion to
Date
. Here is an example to convert your date frommm/dd/yyyy
toISODate
:For Unix timestamp (millis from epoch), you can call
getTime()
fromDate
:Note that these dates will be converted into UTC format, so you may want to change temporarily your timezone before doing your conversion
You can also use bulk update if you want to optimize update performance
You can also just convert your date to
yyyy-mm-dd
which will preserve sorting (check this post). The following will decompose your date field intoday
,month
andyear
, set date field with the new format and write output in a new collection namedtest2
: