sepehr
12/4/2013 - 2:15 PM

MongoDB: Fix string-typed MongoDate fields

MongoDB: Fix string-typed MongoDate fields

db.jobseekers
	// Find all documents with a string-typed date field
	.find({date: {$type: 2}})
	
	// Convert each date field to a ISODate based on its "created_on" raw timestamp
	.forEach(function(doc) {
		// The JSON <date> representation that Mongo expects is a 64-bit signed 
		// integer representing milliseconds since the epoch. 
		// We need to multiply the unixtime seconds value by 1000.
		//
		// @see: http://docs.mongodb.org/manual/core/import-export/#data-type-fidelity
		doc.date = new Date(doc.created_on * 1000);
		
		// Save modified doc
		db.jobseekers.save(doc);
	});