How to remove duplicate entries by two keys in MongoDB?



To remove duplicate entries by two keys, use aggregate() in MongoDB. Let us create a collection with documents −

> db.demo108.insertOne({"Value1":23,"Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3e49fd5fd66da214477") } > db.demo108.insertOne({"Value1":23,"Value2":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f29fd5fd66da214478") } > db.demo108.insertOne({"Value1":23,"Value2":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2ee3f59fd5fd66da214479") }

Display all documents from a collection with the help of find() method −

> db.demo108.find();

This will produce the following output −

{ "_id" : ObjectId("5e2ee3e49fd5fd66da214477"), "Value1" : 23, "Value2" : 24 } { "_id" : ObjectId("5e2ee3f29fd5fd66da214478"), "Value1" : 23, "Value2" : 25 } { "_id" : ObjectId("5e2ee3f59fd5fd66da214479"), "Value1" : 23, "Value2" : 24 }

Following is the query to remove duplicate entries by two keys in MongoDB −

> db.demo108.aggregate([{ "$sort": { "_id": 1 } }, ... { ...    "$group": { ...       "_id": { "Value1": "$Value1", "Value2": "$Value2" }, ...       "doc": { "$first": "$$ROOT" } ...    } ... }, ... { "$replaceRoot": { "newRoot": "$doc" } }, ... { "$out": "demo108" }]);

Display all documents from a collection with the help of find() method −

> db.demo108.find();

This will produce the following output −

{ "_id" : ObjectId("5e2ee3f29fd5fd66da214478"), "Value1" : 23, "Value2" : 25 } { "_id" : ObjectId("5e2ee3e49fd5fd66da214477"), "Value1" : 23, "Value2" : 24 }
Updated on: 2020-03-30T11:47:54+05:30

784 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements