Skip to content

Instantly share code, notes, and snippets.

@freeCodeCamp
Last active August 29, 2015 14:13
Show Gist options
  • Save freeCodeCamp/a6441077db4c08560c1a to your computer and use it in GitHub Desktop.
Save freeCodeCamp/a6441077db4c08560c1a to your computer and use it in GitHub Desktop.
Sample Mongo migrations
db.users.find({'profile.picture': /twimg.*_normal/}).forEach( function(user) {
var picture = user.profile.picture.replace(/_normal/, '');
user.profile.picture = picture;
db.users.save( user );
print(user.profile);
});
db.users.find({email: /@twitter.com/}).forEach( function(user) {
var username = user.email.replace('@twitter.com', '');
user.profile.username = username;
user.profile.twitterHandle = username;
user.email = undefined;
db.users.save( user );
print(user.profile.username, user.email);
});
db.users.find({'profile.username': {$exists: false}}).forEach( function(user) {
var username = "fccuser" + index++ + Math.floor((Math.random() * userCount));
user.profile.username = username;
db.users.save( user );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment