I have a collection in MongoDB where there are around (~200k records). My sample record would look like,
{
name:String,
slug:String,
metaDes:String,
content:String,
parentID:String,
thumbnail:String,
display:Boolean,
}
I am having a lot of duplicate records in the collection having same slug
I want to remove duplicate records based on slug
Is there any fast way to remove all duplicates with mongoose(Nodejs)?
Thanks!
I have a collection in MongoDB where there are around (~200k records). My sample record would look like,
{
name:String,
slug:String,
metaDes:String,
content:String,
parentID:String,
thumbnail:String,
display:Boolean,
}
I am having a lot of duplicate records in the collection having same slug
I want to remove duplicate records based on slug
Is there any fast way to remove all duplicates with mongoose(Nodejs)?
Thanks!
1 Answer
Reset to default 7Remove duplicate records in the collection having the same slug
db.table.aggregate([
{
"$group": {
_id: {slug: "$slug"},
slugs: { $addToSet: "$_id" } ,
count: { $sum : 1 }
}
},
{
"$match": {
count: { "$gt": 1 }
}
}
]).forEach(function(doc) {
doc.slugs.shift();
db.table.remove({
_id: {$in: doc.slugs}
});
})
Refarnce link