Skip to content

Instantly share code, notes, and snippets.

@sepehr
Last active December 30, 2015 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sepehr/7788082 to your computer and use it in GitHub Desktop.
Save sepehr/7788082 to your computer and use it in GitHub Desktop.
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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment