how to automatically get rid of duplicates if found in mongodb code example
Example: mongodb delete duplicate documents
var duplicates = [];
db.collectionName.aggregate([
{ $match: {
name: { "$ne": '' }
}},
{ $group: {
_id: { name: "$name"},
dups: { "$addToSet": "$_id" },
count: { "$sum": 1 }
}},
{ $match: {
count: { "$gt": 1 }
}}
],
{allowDiskUse: true}
)
.forEach(function(doc) {
doc.dups.shift();
doc.dups.forEach( function(dupId){
duplicates.push(dupId);
}
)
})
printjson(duplicates);
db.collectionName.remove({_id:{$in:duplicates}})