ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Remove Duplicate Entries from MongDB Collection

    IT Discussion
    mongodb nosql aggregate
    2
    3
    781
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • scottalanmillerS
      scottalanmiller
      last edited by

      In MongoDB 2, there were easy ways to eliminate duplicates in collections. But now with MongoDB 3 and later, we need to do something a little more creative. This one is tested on MongoDB 3.4.

      If you followed my other post about how to find the duplicates in the first place, we will be continuing on from that.

      https://mangolassi.it/topic/16771/find-duplicate-value-entries-in-mongodb-collection

      db.myCollection.aggregate([ 
          {$group: 
              { _id: "$field_to_search", "dups": { "$push": "$_id" }, count: { $sum: 1}}}, {$match: { count: { $gt: 1 }}}]).forEach(function(doc) { doc.dups.shift(); db.myCollection.remove({ "_id": {"$in": doc.dups }}); });
      
      1 Reply Last reply Reply Quote 1
      • scottalanmillerS
        scottalanmiller
        last edited by

        You can add an index as well, to avoid having duplicates occur in the future:

        db.myCollection.createIndex({"my_field_to_be_unique":1},{unique:true})
        
        bbigfordB 1 Reply Last reply Reply Quote 0
        • bbigfordB
          bbigford @scottalanmiller
          last edited by

          @scottalanmiller said in Remove Duplicate Entries from MongDB Collection:

          You can add an index as well, to avoid having duplicates occur in the future:

          db.myCollection.createIndex({"my_field_to_be_unique":1},{unique:true})
          

          Good idea.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post