Last Updated: September 27, 2021
·
30K
· fsamir

MongoDB: find duplicates in a field

This little script helps find duplicated data in a MongoDB field.

m = function () {
    emit(this.my_field, 1);
}
r = function (k, vals) {
   return Array.sum(vals);
}
res = db.MyCollection.mapReduce(m,r, { out : "my_output" });
db[res.result].find({value: {$gt: 1}});

1 Response
Add your response

db.MyCollection.aggregate([
{$group : { id: "$myfield" , count : { $sum: 1}}},
{$match : { count : { $gt : 1 } }} ])

over 1 year ago ·