Syntax error Check for duplicates in an array in MongoDB?

Check for duplicates in an array in MongoDB?



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" ] }
Updated on: 2020-07-01T06:46:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements