Last active
December 30, 2015 06:18
-
-
Save sepehr/7788082 to your computer and use it in GitHub Desktop.
MongoDB: Fix string-typed MongoDate fields
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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