
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }
Advertisements