MongoDB aggregation group and remove duplicate array values?

To group documents and remove duplicate values from arrays in MongoDB, use the aggregation framework with $unwind to deconstruct arrays and $addToSet within $group to collect unique values.

Syntax

db.collection.aggregate([
    { $unwind: "$arrayField" },
    { $group: { 
        _id: "$groupByField", 
        arrayField: { $addToSet: "$arrayField" },
        otherField: { $first: "$otherField" }
    }}
]);

Sample Data

db.demo649.insertMany([
    { "_id": 101, "Names": ["John", "Bob", "Bob", "Robert"], "CountryName": "US" },
    { "_id": 102, "Names": ["John", "Robert"], "CountryName": "UK" }
]);
{
    "acknowledged": true,
    "insertedIds": { "0": 101, "1": 102 }
}

Display all documents from the collection ?

db.demo649.find();
{ "_id": 101, "Names": ["John", "Bob", "Bob", "Robert"], "CountryName": "US" }
{ "_id": 102, "Names": ["John", "Robert"], "CountryName": "UK" }

Example: Remove Duplicates Using Aggregation

Group by document ID and remove duplicate values from the Names array ?

db.demo649.aggregate([
    { $unwind: "$Names" },
    { $group: { 
        _id: "$_id", 
        Names: { $addToSet: "$Names" },
        CountryName: { $first: "$CountryName" }
    }}
]);
{ "_id": 102, "Names": ["Robert", "John"], "CountryName": "UK" }
{ "_id": 101, "Names": ["Robert", "Bob", "John"], "CountryName": "US" }

How It Works

  • $unwind creates separate documents for each array element
  • $group groups documents back by _id
  • $addToSet collects unique values, automatically removing duplicates
  • $first preserves the original value of non-array fields

Conclusion

The $unwind and $addToSet combination effectively removes duplicates from arrays while preserving document structure. This approach works for any array field that needs deduplication.

Updated on: 2026-03-15T03:16:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements