最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论