mongodb delete duplicate documents
var duplicates = [];
db.collectionName.aggregate([
{ $match: {
name: { "$ne": '' } // discard selection criteria
}},
{ $group: {
_id: { name: "$name"}, // can be grouped on multiple properties
dups: { "$addToSet": "$_id" },
count: { "$sum": 1 }
}},
{ $match: {
count: { "$gt": 1 } // Duplicates considered as count greater than one
}}
],
{allowDiskUse: true} // For faster processing if set is larger
) // You can display result until this and check duplicates
.forEach(function(doc) {
doc.dups.shift(); // First element skipped for deleting
doc.dups.forEach( function(dupId){
duplicates.push(dupId); // Getting all duplicate ids
}
)
})
// If you want to Check all "_id" which you are deleting else print statement not needed
printjson(duplicates);
// Remove all duplicates in one go
db.collectionName.remove({_id:{$in:duplicates}})
Also in JavaScript:
- how to make a translator in python
- how to compare two arrays javascript
- dart lambda expression
- how to access router from the store vue
- how many characters can fit in 1 line of div
- javascript change color
- js know size of screen displayed
- paypal cordova can not open popup window - blocked
- remove floating point javascript
- face-api.js
- javascript input onchange get value
- jquery add style
- .gitignore nodejs
- function para mter js
- javascript leave page warning
- match js
- find a single element in array of objects javascript
- express-jwt error algorithms should be set
- js loop trough map
- first n elements of array js
- what is promise in javascript
- not getting webhook post request json data in my laravel controller
- dynamically add images to carousel
- how to get a String in dart