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
    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: Logo

You are commenting using your 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