Remove duplicate documents from collection in mongodb 3.X

From mongodb 2.6 “dropDups” option with creation of unique index is deprecated.

Use the below command to remove the duplicate documents in mongodb.

db..find({}, {“”:1}).sort({_id:1}).forEach
(
function(doc)
{
    db..remove({_id:{$gt:doc._id}, <attribute/column name:doc.<attribute/column name});
}
)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s