Converting string to date in mongodb

user883257 picture user883257 · Jun 8, 2012 · Viewed 98.6k times · Source

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?

Answer

Ciges picture Ciges · Jun 4, 2013

In my case I have succeed with the following solution for converting field ClockInTime from ClockTime collection from string to Date type:

db.ClockTime.find().forEach(function(doc) { 
    doc.ClockInTime=new Date(doc.ClockInTime);
    db.ClockTime.save(doc); 
    })