Skip to content

Instantly share code, notes, and snippets.

@noirbizarre
Created November 13, 2018 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noirbizarre/cd5a7adc20e8323ee080d864bae1bd83 to your computer and use it in GitHub Desktop.
Save noirbizarre/cd5a7adc20e8323ee080d864bae1bd83 to your computer and use it in GitHub Desktop.
Deduplicate users
db.user.aggregate([
{ $sort: { created_at: 1 } },
{
"$group": {
_id: "$email",
dups: { $addToSet: "$_id" } ,
count: { $sum : 1 }
}
},
{
"$match": {
count: { "$gt": 1 }
}
}
]).forEach(function(doc) {
doc.dups.shift();
var res = db.user.remove({
_id: {$in: doc.dups}
});
print(`Removed ${res.deletedCount} duplicate for ${doc._id}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment