Check for duplicates in an array in MongoDB?

MongoDBDatabaseBig Data Analytics

To check for duplicates in an array, use aggregate() in MongoDB. Let us create a collection with documents −

> db.demo756.insertOne({"SubjectName":["MySQL","MongoDB","Java"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eb01e0d5637cd592b2a4add")
}
> db.demo756.insertOne({"SubjectName":["MongoDB","MySQL","MongoDB","C","C+","MySQL"]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5eb01e2b5637cd592b2a4ade")
}

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

> db.demo756.find();

This will produce the following output −

{ "_id" : ObjectId("5eb01e0d5637cd592b2a4add"), "SubjectName" : [ "MySQL", "MongoDB", "Java" ] }
{ "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL", "MongoDB", "C", "C+", "MySQL" ] }

Following is the query to check for duplicates in an array −

> db.demo756.aggregate([
...    {"$project": {"SubjectName":1}},
...    {"$unwind":"$SubjectName"},
...    {"$group": {"_id":{"_id":"$_id", "Name":"$SubjectName"}, "count":{"$sum":1}}},
...    {"$match": {"count":{"$gt":1}}},
...    {"$group": {"_id": "$_id._id", "SubjectName":{"$addToSet":"$_id.Name"}}}
... ])

This will produce the following output −

{ "_id" : ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName" : [ "MongoDB", "MySQL" ] }
raja
Published on 01-Jul-2020 10:14:52
Advertisements