mongodb count documents filter out duplicates code example
Example 1: 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}})
Example 2: duplicated documens mongodb
db.users.aggregate([
{$group: {
_id: {email: "$email"},
uniqueIds: {$addToSet: "$_id"},
count: {$sum: 1}
}
},
{$match: {
count: {"$gt": 1}
}
},
{$sort: {
count: -1
}
}
]);