Change the data type dynamically in mongo

I'm using mongo DB to initially store data I scraped from the web. And like all good projects, I realized that I was storing a date value as a string. I corrected the code, but now I have a field that haves the values stored as a string and date object.

use scraped_db

var cursor = db.scraped_collection.find({'datePosted': {$type: 'string'}});
while (cursor.hasNext()) {
    var doc = cursor.next();
    db.scraped_collection.update(
             { '_id': doc._id},
             { $set: {datePosted: new ISODate(doc.datePosted) } }
    )

Simple and straight forward.

Happy hacking.

Comments

Comments powered by Disqus