Last active
October 24, 2023 19:11
-
-
Save kulakoff/855e887c3395df83fedacf849f21d939 to your computer and use it in GitHub Desktop.
поиск дубликатов записей mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//mongo find duplicates by SN | |
var duplicates = []; | |
db.devices.aggregate([ | |
{ $match: { | |
name: { "$ne": '' } // discard selection criteria | |
}}, | |
{ $group: { | |
_id: { name: "$_deviceId._SerialNumber"}, // can be grouped on multiple properties | |
dups: { "$addToSet": { | |
_id: "$_id", | |
_ProductClass: "$_deviceId._ProductClass", | |
_Serial: "$_deviceId._SerialNumber" | |
}}, | |
count: { "$sum": 1 } | |
}}, | |
{ $match: { | |
count: { "$gt": 1 } // Duplicates considered as count greater than one | |
}} | |
], | |
{allowDiskUse: true} // For faster processing if set is larger | |
) | |
.forEach(function(doc) { | |
doc.dups.shift(); // First element skipped for deleting | |
doc.dups.forEach( function(dupId){ | |
if (dupId._ProductClass === "OT2800") { | |
duplicates.push(dupId._id); | |
//console.log(dupId._id) | |
} | |
// duplicates.push(dupId); | |
} | |
) | |
}) | |
console.log(duplicates.length) | |
printjson(duplicates); | |
//db.devices.deleteOne({_id:{$in:duplicates}}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment