13

I have my ISODate in mongo as ISODate and I want to just that in string format with a specific datetime format.

Here is the ISODate:

ISODate("2020-04-24T11:41:47.280Z")

Expected Result:

"2020-04-24T11:41:47.280Z"

I want this to be happened on mongodb only as many of my services are expecting in this format, and I don't want to make changes in all services as its a tedious job.

Manu
  • 3,979
  • 7
  • 21
  • 25
  • Are you doing this from the `mongo` shell, or a specific driver? `ISODate()` is just a convenience wrapper around a standard JavaScript `Date` object so you can do: `ISODate("2020-04-24T11:41:47.280Z").toISOString()`. If you are using JavaScript I'd suggest [moment.js](http://momentjs.com/) for more advanced formatting. – Stennie Dec 19 '14 at 06:07
  • 1
    are you seeking a way of converting all your data from ISODate to a string. or keeping it as a date in mongodb and parsing it to a string when requesting in your backend. – Gabe Rainbow Dec 19 '14 at 06:10
  • I am running a update script in mongo to convert the string date to ISODate, here is the script which i am running db.events.find().forEach(function(element){ if(element.t && typeof element.t === "string"){ element.t = ISODate(element.t); }}) Here element.t will be "2020-04-24T11:41:47.280Z". after running this script it will be converted to ISODate("2020-04-24T11:41:47.280Z"). now i want to convert it back to same format in mongo "2020-04-24T11:41:47.280Z". i cant use moment in mongodb. – Manu Dec 19 '14 at 06:19
  • @Manu Please edit your question to include the code. Code doesn't format well in comments. – JohnnyHK Dec 19 '14 at 06:24
  • Quick question. Will you still be able to do a date range query with your expected result stored in a string instead of a date object? – codex Jan 26 '18 at 14:38

6 Answers6

22

I got the expected result while i was trying the following.

ISODate("2020-04-24T11:41:47.280Z").toJSON()

This will give me back the string

"2020-04-24T11:41:47.280Z"
Manu
  • 3,979
  • 7
  • 21
  • 25
5

perhaps simply convert a date into string? this has less to do with mongo than js. moment is awesome but unusable in a mongo shell script.

db.events.find().forEach(function(doc) {     
   //   printjson ("Document is " + doc);    
   var isoDate = doc.t;   // t is correct key?     
   var isoString = isoDate.toISOString()    
   // update the collection with string using a new key    
   db.events.update(
        {"_id":doc._id},
        {
          $set: {"iso_str":isoString}
        }
   );
   // or overwrite using 't' key      db.events.update({"_id":doc._id},{$set:{"t":isoString}});
})
Shubham Chadokar
  • 2,520
  • 1
  • 24
  • 45
Gabe Rainbow
  • 3,658
  • 4
  • 32
  • 42
1

I just came across this issue. There isn't anything built into ISODate turns out. So I'm converting the ISODate to JSON text, and then I do a substring to get the part I want. You can also use method on ISODate to get year, month, date separately and then combine them.

function formatDate(isoDate){
    return isoDate.toJSON().substr(9, 20);
}
cjmling
  • 6,896
  • 10
  • 43
  • 79
user2793390
  • 741
  • 7
  • 29
0

I assume that what you need is the dateToString function https://docs.mongodb.com/manual/reference/operator/aggregation/dateToString/

  • _Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline._ – Bugs Feb 28 '17 at 10:27
0

If you have mixed data types[string & ISODate()] in an attribute & still want to normalize them to one data type e.g., 'string' then

typeof attr_val === 'string' ?  attr_val : attr_val.toJSON()

References:

Abhijeet
  • 8,561
  • 5
  • 70
  • 76
0
db.inventries.find().forEach(function (doc) {
    let mydate = new Date(doc.date).toISOString(); 
    db.inventries.update({ "_id": doc._id }, { $set: { "date": ISODate(mydate) } })
})
Shashwat Gupta
  • 5,071
  • 41
  • 33