Created
November 13, 2018 12:38
-
-
Save noirbizarre/cd5a7adc20e8323ee080d864bae1bd83 to your computer and use it in GitHub Desktop.
Deduplicate users
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
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