Skip to content

Instantly share code, notes, and snippets.

@alanderex
Created August 31, 2016 20:26
Show Gist options
  • Select an option

  • Save alanderex/5efb6e48218b80654e7cc862e9385438 to your computer and use it in GitHub Desktop.

Select an option

Save alanderex/5efb6e48218b80654e7cc862e9385438 to your computer and use it in GitHub Desktop.
// assuming the latest vcersion 3.2 for this solution <3.2 difers
// prerequisites: all docs contain both fields
// In the real world, I'd add a search with $exists as well and check for the data type $type: 2
// plus double check the format matches MM/DD/YYYY
function makeToDate(d) {
var thedate = new Date(d);
return thedate
}
var updates = []
var mycursor = db.mycollection.find({}, {
date_sent_to_company: 1,
date_received: 1
})
mycursor.forEach(function(d) {
var date_sent_to_company = makeToDate(d.date_sent_to_company);
var date_received = makeToDate(d.date_received);
updates.push({
"updateOne": {
"filter": {
"_id": d._id
},
"update": {
"$set": {
"date_sent_to_company": date_sent_to_company,
"date_received": date_received
}
}
}
});
if (updates.length >= 100) {
db.mycollection.bulkWrite(updates);
updates = [];
}
})
if (updates.length > 0) {
db.mycollection.bulkWrite(updates);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment