简体   繁体   中英

How to remove duplicates in mongoDB with mongoose(NodeJS)

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM