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
How to remove duplicate record in MongoDB 3.x?
To remove duplicate record, use aggregate(). Let us create a collection with documents −
> db.demo438.insertOne({"FirstName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e775c37bbc41e36cc3caea1")
}
> db.demo438.insertOne({"FirstName":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e775c3dbbc41e36cc3caea2")
}
> db.demo438.insertOne({"FirstName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e775c40bbc41e36cc3caea3")
}
> db.demo438.insertOne({"FirstName":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e775c44bbc41e36cc3caea4")
}
> db.demo438.insertOne({"FirstName":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e775c47bbc41e36cc3caea5")
}
Display all documents from a collection with the help of find() method −
> db.demo438.find();
This will produce the following output −
{ "_id" : ObjectId("5e775c37bbc41e36cc3caea1"), "FirstName" : "Chris" }
{ "_id" : ObjectId("5e775c3dbbc41e36cc3caea2"), "FirstName" : "David" }
{ "_id" : ObjectId("5e775c40bbc41e36cc3caea3"), "FirstName" : "Chris" }
{ "_id" : ObjectId("5e775c44bbc41e36cc3caea4"), "FirstName" : "Bob" }
{ "_id" : ObjectId("5e775c47bbc41e36cc3caea5"), "FirstName" : "David" }
Following is the query to remove duplicate records in MongoDB 3.x −
> db.demo438.aggregate([ { "$group":{ _id:{FirstName:"$FirstName"}, DuplicateValueIds:{$addToSet:"$_id"} } } ]);
This will produce the following output −
{ "_id" : { "FirstName" : "David" }, "DuplicateValueIds" : [ ObjectId("5e775c47bbc41e36cc3caea5"), ObjectId("5e775c3dbbc41e36cc3caea2") ] }
{ "_id" : { "FirstName" : "Bob" }, "DuplicateValueIds" : [ ObjectId("5e775c44bbc41e36cc3caea4") ] }
{ "_id" : { "FirstName" : "Chris" }, "DuplicateValueIds" : [ ObjectId("5e775c40bbc41e36cc3caea3"), ObjectId("5e775c37bbc41e36cc3caea1") ] }Advertisements