-
-
Save Signorini/a9ff117605d31676296c2b089e2a5794 to your computer and use it in GitHub Desktop.
Remove duplicate entry - MongoDB
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
var cursor = db.getCollection('servers').aggregate([ | |
{ $group: { | |
_id: { name: "$hostname" }, // replace `name` here twice | |
uniqueIds: { $addToSet: "$_id" }, | |
count: { $sum: 1 } | |
} }, | |
{ $match: { | |
count: { $gte: 2 } | |
} }, | |
{ $sort : { count : -1} }, | |
{ $limit : 1000 } | |
]); | |
ids = cursor.map(function (doc) { | |
return { | |
_id: doc._id, // replace `name` here twice | |
uniqueIds: doc.uniqueIds.slice(1), | |
count: doc.count | |
}}); | |
rmv = ids.map(function(doc) {return db.getCollection('servers').remove({"_id": { "$in": doc.uniqueIds }});}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment