javascript - How to remove duplicates in mongoDB with mongoose(NodeJS) - Stack Overflow

I have a collection in MongoDB where there are around (~200k records). My sample record would look like

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!

Share Improve this question asked Apr 12, 2020 at 6:37 vy.phamvy.pham 6211 gold badge10 silver badges23 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Remove 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745181521a4615439.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信