简体   繁体   English

如何使用猫鼬(NodeJS)删除 mongoDB 中的重复项

[英]How to remove duplicates in mongoDB with mongoose(NodeJS)

I have a collection in MongoDB where there are around (~200k records).我在 MongoDB 有一个集合,其中大约有(约 20 万条记录)。 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我在集合中有很多重复的记录有相同的slug

I want to remove duplicate records based on slug Is there any fast way to remove all duplicates with mongoose(Nodejs)?我想根据slug删除重复记录有没有什么快速的方法可以用 mongoose(Nodejs) 删除所有重复项? Thanks!谢谢!

Remove duplicate records in the collection having the same slug删除集合中具有相同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参考链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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