Check for duplicates in an array in MongoDB?

To check for duplicates in an array in MongoDB, use the $unwind and $group operators within an aggregation pipeline to identify array elements that appear more than once in the same document.

Syntax

db.collection.aggregate([
    { $project: { arrayField: 1 } },
    { $unwind: "$arrayField" },
    { $group: { _id: { _id: "$_id", value: "$arrayField" }, count: { $sum: 1 } } },
    { $match: { count: { $gt: 1 } } },
    { $group: { _id: "$_id._id", duplicates: { $addToSet: "$_id.value" } } }
]);

Sample Data

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

Display Collection Data

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

Check for Array Duplicates

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" } } }
]);
{ "_id": ObjectId("5eb01e2b5637cd592b2a4ade"), "SubjectName": ["MongoDB", "MySQL"] }

How It Works

  1. $project ? Selects only the array field to process
  2. $unwind ? Deconstructs the array into separate documents for each element
  3. $group ? Groups by document ID and array value, counting occurrences
  4. $match ? Filters for counts greater than 1 (duplicates)
  5. $group ? Regroups by document ID, collecting duplicate values

Conclusion

Use aggregation with $unwind and $group to efficiently detect duplicate values within arrays. Only documents containing duplicates will appear in the final results.

Updated on: 2026-03-15T03:57:01+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements