Skip to content

Instantly share code, notes, and snippets.

@Signorini
Created November 29, 2017 14:37
Show Gist options
  • Save Signorini/a9ff117605d31676296c2b089e2a5794 to your computer and use it in GitHub Desktop.
Save Signorini/a9ff117605d31676296c2b089e2a5794 to your computer and use it in GitHub Desktop.
Remove duplicate entry - MongoDB
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