Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created September 20, 2011 07:37
Show Gist options
  • Save jeffreyiacono/1228573 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/1228573 to your computer and use it in GitHub Desktop.
(budget) find / replace for mongodb
// For a collection "things" that has multiple documents of the form:
// { "_id" : ObjectId("4e5c47d1b0207a1ad3000001"),
// "filename" : "uploads/development/user/4dbf2b0ab0207a2e8a000001/avatar/avatar.png",
// "other_data" : "foo"
// }, etc.
// we want to replace "development" in "filename" with "production".
// Run via mongo shell:
db.things.find().forEach(function(e) {
var parts = e.filename.split('development');
e.filename = parts[0] + 'production' + parts[1];
db.things.save(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment