How to Convert mongodb ISODate to string in mongoDB?

前端 未结 4 1300
悲哀的现实
悲哀的现实 2021-01-12 15:02

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(\"20         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 15:20

    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} );
    })
    

提交回复
热议问题