w01ukg
Last Updated: January 25, 2018
·
14.9K
· 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

18098

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

over 1 year ago ·