MongoDB & Mongoid: How to Find Duplicate Entries

CategoriesRuby on Rails


From: https://stackoverflow.com/questions/27482840/ruby-finding-duplicates-in-mongodb/27488317



results = JobNumber.collection.aggregate([
  { "$group" => {
    _id: { "number" => "$number"},
    recordIds: {"$addToSet" => "$_id" },
    count: { "$sum" => 1 }
  }},
  { "$match" => {
    count: { "$gt" => 1 }
  }}
])

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.