Skip to content

Instantly share code, notes, and snippets.

@DonerKebab
Created December 15, 2020 09:34
Show Gist options
  • Save DonerKebab/e59fc1de73710d8ab9c80b8f15916bb6 to your computer and use it in GitHub Desktop.
Save DonerKebab/e59fc1de73710d8ab9c80b8f15916bb6 to your computer and use it in GitHub Desktop.
remove duplicate record in mongodb
try {
await client.db(dbName).collection(`${collectionNamePrefix}`).aggregate([
{
"$group": {
_id: {ICCID: "$ICCID"}, // field which have duplicate value
dups: { $addToSet: "$_id" } ,
count: { $sum : 1 }
}
},
{
"$match": {
count: { "$gt": 1 }
}
}
]).forEach( async (doc) => {
console.log(doc)
doc.dups.shift();
await client.db(dbName).collection(`${collectionNamePrefix}`).deleteMany({
_id: {$in: doc.dups}
});
})
} catch (error) {
console.log(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment