Mongodb - convert the datetime to ISOdate format -
i want convert existing string date new iso date format. used scripts not enough convert time associated date. ex: 2013-01-10 12:22:22 converted isodate("2013-01-10t06:00:00z"). here time not converting. please help.
var cursor = db.customer.find({"created_at": {"$exists": true, "$type": 2 }}); while (cursor.hasnext()) { var doc = cursor.next(); var parts = doc.created_at.split("-"); var dt = new date( parseint(parts[0], 10), // year parseint(parts[1], 10) - 1, // month parseint(parts[2], 10)// day ); db.customer.update( {"_id": doc._id}, {"$set": {"created_at": dt}} ) }
simply use new isodate() :
new isodate("2013-01-10 12:22:22 ") isodate("2013-01-10t12:22:22z")
in full example :
var cursor = db.customer.find({"created_at": {"$exists": true, "$type": 2 }}); while (cursor.hasnext()) { var doc = cursor.next(); db.customer.update( {"_id": doc._id}, {"$set": {"created_at": new isodate(doc.created_at)}} ) }
Comments
Post a Comment