showing results for - "mongodb delete duplicate documents"
Nicola
28 Feb 2017
1var duplicates = [];
2db.collectionName.aggregate([
3  { $match: { 
4    name: { "$ne": '' }  // discard selection criteria
5  }},
6  { $group: { 
7    _id: { name: "$name"}, // can be grouped on multiple properties 
8    dups: { "$addToSet": "$_id" }, 
9    count: { "$sum": 1 } 
10  }}, 
11  { $match: { 
12    count: { "$gt": 1 }    // Duplicates considered as count greater than one
13  }}
14],
15{allowDiskUse: true}       // For faster processing if set is larger
16)               // You can display result until this and check duplicates 
17.forEach(function(doc) {
18    doc.dups.shift();      // First element skipped for deleting
19    doc.dups.forEach( function(dupId){ 
20        duplicates.push(dupId);   // Getting all duplicate ids
21        }
22    )    
23})
24// If you want to Check all "_id" which you are deleting else print statement not needed
25printjson(duplicates);     
26// Remove all duplicates in one go    
27db.collectionName.remove({_id:{$in:duplicates}})  
similar questions
monogodb drop collection
queries leading to this page
mongodb script remove duplicateshow to remove duplicate documents from a mongo colectiondelete repeated mongomongo drop duplicates if all fieldshow to prevent duplicate data in mongodb collectioneliminate duplicates in mongodelete duplicate records in mongodb cloudhow to cut duplicates mongodbdelete almost same documents mongodbmongodb drop duplicateshow to remove duplicates in mongodb collection in golangdelete duplicates mongodbcron script mongodb to remove duplicate valuesmongodb duplicate removedelete duplicates in mongodbmangodb remove duplicate datahow to delete duplicates mongodb lef oneremov the duplicate ids in mongodbdrop duplicates mongodbscriptt to remove duplicate usets un mongoremove duplicant in mongodbprevent duplicates in mongodbmongodb aggregate remove duplicates entriesmongodb delete duplicate document pythonmongodb remove duplicates querydelete duplicates from mongodb collectionmongodb delete duplicates emailsmongodb delete duplicate by fieldremove duplicate records in mongodbhow to remove duplicate values from database mongodbprevent duplicate entries in mongodbdoes mongodb remove duplicatesmongodb delete duplicate in existing collectionremove duplicates from collection mongodbtake out duplciates mongodbhow delete duplicates pymongohow to prevent duplicate entries in mongodbremove duplicate values in mongodbremove duplicates mongo 4delete multiple duplicate records in mongodbmongodb remove duplicates from arraymongodb remove duplicate documentmongodb atlas remove all duplicate recordsremove duplicate by key mongodbhow to avoid duplicate values in mongodbmongodb remove duplicaitonshow to remove duplicate doc in mongodbhow to remove duplicates in mongodbmongo remove duplicatesdrop duplicate data from db mongoremove duplicates from db mongomongodb delete dublicate rowmongodb insertmany avoid duplicateremove duplicates in mongodb collectiondelete duplicate items mongodbdelete duplicate from mongodbfind duplicates and delete in mongodbdelete duplicate items in document mongodbmongodb delete duplicate documentshow to automatically get rid of duplicates if found in mongodbhow to prevent duplicates in mongodbremove duplicate data in mongodbmongodb remove duplicates by idhow to remove duplicate records in mongodb querymongoose remove duplicatesavoid duplicate document creation mongodelete duplicate mongodbpymongo function to delete duplicatesmongodb delete duplicate fieldhow to fix duplicates in mongodbremove the same data mongodbremove duplicates in the collection mongodbhow to delete duplicates in mongodb atlasdrop duplicates mongo dbmongodb remove duplicates by fieldmongodb remove duplicates from collectionhow to remove duplicate in mongo dbhow to remove duplicate data in mongodb consodelete all duplicate keys mongohow to avoid duplicate entries in mongodbonly have unque element in db and remove duplicates mongomongodb updateone delete duplicate fieldsmongodb remove all duplicates with different idhow to remove duplicates from mongodbmongodb match remove duplciatesmongodb remove duplicatesmongodb avoid duplicateshow to remove duplicate elements from array mongodbhow to remove duplicate data using djagngo orm with mongodbmongodb unqiue index remove duplicatemongodb find and delete duplicatesprevent duplicates on mongodbdelete duplicate element in mongodbmongodb remove all duplicatesmongodb aggregate remove duplicates from arrayfind duplicates in the collection and delete in mongodbmongodb remove duplicatedmongodb remove duplicates by multiple fieldsdelete second entry in mongodbmongodb 3 o remove duplicate documents by id from a collectionhow to delete duplicate documents in mongodbmongodb remove duplicates valuesdelete all duplicate docs in mongodbavoid duplicates mongodbhow to remove one duplicate rows in mongodbdeleting duplicates in mongodb c ollectionremove the duplicates in mongodbdelete duplicate records in mongodbmongodb count documents filter out duplicatesremove duplicates mongodbmongo drop duplicatesduplicate in mongodbhow to remove duplicate entries in mongodbremove duplicate mongodbremove duplicates mongodb while fetchingfind duplicate mongodb removemongodb find remove duplicatesmongodb duplicate deletionmongodb find and remove duplicate documentsmongodb prevent duplicates in upsetmongodb remove duplicatehow to stop duplicate entry in mongodbmongodb aggregate remove duplicatesremove duplicated data from mongodbhow to avoid duplicate data with insertmany mongodbremove duplicates from mongodb resultmongo delete duplicate conditionmongodb remove duplicates from listremove duplicates records in mongodb aggregationmongo remove duplicatemongodb fix dublicatein a collection how to remove duplicates from mongodbremove duplicates based on title mongodbdrop duplicates in mongodbmongodb remove duplicate documents by id from a collectionremove duplicates using a mongo shellremove duplicates records mongdbhow to remove the duplicate from mongodb databasehow to remove the duplicaes in mongodbmongo remove duplicates from listhow to avoid duplicates in mongodbduplicated data delete from mongomongodb 24project remove duplicate db where remove duplicate objects mongodbhow to find and delete duplicate data in mongodbmongodb prevent duplicates in upsertmongodb drop duplicates on insertdelete duplicates mongo shellavoid duplicate entries mongodbmongodb remove documents with duplicate keymongodb duplicate delete onehow to delete duplicate data from mongodb databasefind duplicates in mongodbremove duplicate documents in mongodbmongodb aggregation drop duplicatesdelete duplicate documents in mongodbmongoose remove duplicates docshow to remove duplicate data in mongodbremove all the duplicate values in mongodb querymongodb c 23 remove duplicate resultshow to filter out duplicates from entering database mongodbhow to avoid duplicate records in mongodbremove duplicates from mongodb query resultshow to remove duplicate data in mongodb consoleget rid of duplicates in mongodbremove duplicates from mongodb collectiondelete duplicate documents in mongodb but keep one copymongodb avoid duplicate createhow to avoid inserting duplicate documents mongodbhow to remove duplicate data using with mongodbfind duplicate mongo removeremoving duplicate values in mongodbremove duplicates from mongo collectionmongodb delete duplicateremove duplicates from mongodbremove duplicates after using 24where mongodbmongodb delete duplicate datafind duplicates in the collection and delete it mongodbremove duplicates from mongo dbremove duplicates while fetching data mongodbhow to remove duplicates in mongo aggregateremove duplicate records mongodbmongodb remove duplicate entriesremove duplicate records in mongodb via compasshow to delete duplicates in mongodbdelete duplicate documents mongodbhow to delete duplicates in mongobdhow to remove duplicates in mongodb collectionmongodb 3 0 remove duplicate documents by id from a collectionmongodb aggregate remove duplicates from querymongodb skip duplicatesmongodb delete duplicatesmongodb compass duplicate a collectionmongodb agreggate remove duplicatesdelete duplicate records from mongohow to search for and delete duplicate records in mongodbmongodb find drop duplicateshow to remove duplicate value 3f mongodbmongodb find by field and remove duplicatesremove duplicate doc mongodrop duplicates mongo remove duplicates entries by a field mongomongodb remove duplicate documents from a collectionremove mongodb duplicate by valueremove documents same value mongodbmongodb remove duplicate left oneprevent duplicate entries mongodbmongodb remove duplicate documentsremove duplicates form mongo dbhow to find and delete duplicate index in mongodbexclude duplicates in mongodbmongodb remove dublicatesremove dublicates in mongodbremove duplicates in mongodbmongodb how to delete duplicatesmongodb agreggation remove duplicates mongodb remove duplicate datamongodb prevent duplicatesmongodb agreggation remove duplicate keysdelete duplicates in aggregator mongodbhow to remove duplicate in mongodbmongodb remove duplicates aggregationremove duplicate data from mongodbmongodb delete duplicate documents