How to remove duplicate record in MongoDB 3.x?

MongoDBDatabaseBig Data Analytics

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") ] }
raja
Published on 06-Apr-2020 16:36:44
Advertisements