4

Having an array of emails on a document. How would we find another document in the same collection that shares at least one email in common?

Essentially we have a contacts collection and each contact document has an array of emailAddresses[]. We want to make sure no two contact documents have an email in common with another document, but we're unable to find where this is happening.

Brock
  • 71
  • 4

1 Answers1

6
db.collection.aggregate([
{"$unwind" : "$emails"},
{$group  : {"_id" : "$emails" , "count" :{"$sum" : 1} }},
{"$match" : {"count" : {"$gt" : 1}}}
])

This will result emails which are dupicate

Parshuram Kalvikatte
  • 1,616
  • 4
  • 20
  • 40