Categories
Development MongoDB

How to convert MongoDB ISODate array elements to formatted string elements in mongo shell?

In my MongoDB database I have, introduced by error, some ISODates, and I need to change them to string format.

I have tried to update them one by one with the updateOne, selecting the element of the array where the wrong element is and changing it by the correct string. This works, but I need to do this for many records so I need to automate it.

I need that the current elements similar to ISODate("2013-02-25T16:01:50.742Z") that are inside a collection like db.myCollection.elements.myArray.0.dateare changed to a string format like "2013-02-25 16:01:50".

Also, I need to find, for all the elements of the array, the date, that is in ISO format, convert it into the string one, and update it.

The format of the document in myCollection is like:

{
   elements: 
   [
      { date: ISODate("2019-03-14T02:42:00Z")},
      { date: ISODate("2019-03-14T02:42:00Z")}
   ]
}

Leave an answer

Your email address will not be published. Required fields are marked *