w01ukg
Last Updated: February 25, 2016
·
8.367K
· fsamir
2b25095a2e4a0aa4dfe0cd854006bb8e

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}});
Say Thanks
Respond

1 Response
Add your response

18098
0 naramtaxyxdot bbnncmmg5ryssxt bb99xmmg t xm7snkf4chjhcc7tqv8azqwv  ft5lopawl

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

over 1 year ago ·