Last active
January 14, 2016 10:33
-
-
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
This file contains 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
// 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