
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
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 }
- Related Questions & Answers
- Avoid duplicate entries in MongoDB?
- Program to remove duplicate entries in a list in Python
- Program to remove duplicate entries in a linked list in Python
- Efficient way to remove all entries from MongoDB?
- How to remove duplicate record in MongoDB 3.x?
- Using recursion to remove consecutive duplicate entries from an array - JavaScript
- Using recursion to remove consecutive duplicate entries from an array in JavaScript
- How to remove duplicate values inside a list in MongoDB?
- How to create Python dictionary with duplicate keys?
- MongoDB aggregation group and remove duplicate array values?
- How to generate child keys by parent keys in array JavaScript?
- How to remove an array element by its index in MongoDB?
- How to not allow duplicate entries to be entered a MySQL Table?
- How to remove duplicate rows in an R data frame if exists in two columns?
- MongoDB order by two fields sum?
Advertisements