Skip to content

Instantly share code, notes, and snippets.

@javierfdezg
Last active January 14, 2016 10:33
Show Gist options
  • Save javierfdezg/2831e22f932980341370 to your computer and use it in GitHub Desktop.
Save javierfdezg/2831e22f932980341370 to your computer and use it in GitHub Desktop.
Command to modify the date on all the records of a MongoDB collection
// Command:
// Change database for your db name and collection for your collection name
// Remove limit(1). Is there in order to avoid accidents ;)
mongo database --eval \"db.collection.find().limit(1).forEach(function(doc){ var nd = new Date(doc.created_at); nd.setDate(nd.getDate() + 1); doc.created_at = new ISODate(nd.toISOString()); db.collection.save(doc)});"
/* The function in a more readable fashion :)
function(doc) {
var nd = new Date(doc.created_at);
nd.setDate(nd.getDate() + 1);
doc.created_at = new ISODate(nd.toISOString());
db.collection.save(doc)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment