Find duplicate inside an array mongodb
You're not far off just some minor tweaks needed:
db.getCollection("users").aggregate( [ { "$unwind" : "$profile.organizations" }, { "$group" : { "_id" : { "dup" : "$profile.organizations", "id" : "$_id" }, "count" : { "$sum" : 1.0 } } }, { "$match" : { "count" : { "$gt" : 1.0 } } }, { "$group" : { _id: "$_id.id", Dupes: {$push: "$_id.dup"} } } ], );
You can use below aggregation
db.collection.aggregate([ { "$project": { "Dupes": { "$filter": { "input": { "$setUnion": ["$profile.Organizations"] }, "as": "s", "cond": { "$gt": [ { "$size": { "$filter": { "input": "$profile.Organizations", "cond": { "$eq": ["$$this", "$$s"] } } }}, 1 ] } } } }}])