Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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
-
$unwindcreates separate documents for each array element -
$groupgroups documents back by_id -
$addToSetcollects unique values, automatically removing duplicates -
$firstpreserves 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.
Advertisements
