Skip to content

Instantly share code, notes, and snippets.

@afeld
Created June 11, 2012 17:35
Show Gist options
  • Save afeld/2911480 to your computer and use it in GitHub Desktop.
Save afeld/2911480 to your computer and use it in GitHub Desktop.
Jux: remove duplicate custom_links
db.users.find({'custom_links.1': {$exists: true}}).snapshot().forEach(function(user){
var urls = {},
keepLinks = [];
user.custom_links.forEach(function(link){
var url = link.value.toString().toLowerCase().replace(/\/$/, '').replace(/^(https?:?\/\/)?(www\. ?)?/, '');
if (!urls[url]){
keepLinks.push(link);
urls[url] = true;
}
});
if (user.custom_links.length !== keepLinks.length){
print(user.username + '--------------------------------------');
printjson(urls);
db.users.update({_id: user._id}, {$set: {custom_links: keepLinks}}, false, false);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment