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
How to remove duplicate values inside a list in MongoDB?
To remove duplicate values from an array field in MongoDB, use the aggregation framework with the $setUnion operator. This operator creates a set union between the array and an empty array, automatically eliminating duplicates.
Syntax
db.collection.aggregate([
{
$project: {
"fieldName": { $setUnion: ["$arrayField", []] }
}
}
]);
Sample Data
db.removeDuplicatesDemo.insertOne({
"InstructorName": "Chris",
"InstructorAge": 34,
"InstructorSubject": [
"Java", "C", "Java", "C++", "MongoDB", "MySQL", "MongoDB"
]
});
{
"acknowledged": true,
"insertedId": ObjectId("5cb9d96c895c4fd159f80807")
}
Verify Original Data
db.removeDuplicatesDemo.find().pretty();
{
"_id": ObjectId("5cb9d96c895c4fd159f80807"),
"InstructorName": "Chris",
"InstructorAge": 34,
"InstructorSubject": [
"Java",
"C",
"Java",
"C++",
"MongoDB",
"MySQL",
"MongoDB"
]
}
Remove Duplicates from Array
db.removeDuplicatesDemo.aggregate([
{
$project: {
"InstructorName": 1,
"InstructorAge": 1,
"InstructorSubject": { $setUnion: ["$InstructorSubject", []] }
}
}
]).pretty();
{
"_id": ObjectId("5cb9d96c895c4fd159f80807"),
"InstructorName": "Chris",
"InstructorAge": 34,
"InstructorSubject": [
"C",
"C++",
"Java",
"MongoDB",
"MySQL"
]
}
How It Works
The $setUnion operator performs a set union between $InstructorSubject and an empty array []. Since set operations automatically remove duplicates, this effectively deduplicates the original array while preserving unique values in alphabetical order.
Conclusion
Use $setUnion with an empty array in the aggregation pipeline to remove duplicates from array fields. This method is efficient and maintains the unique values in sorted order.
Advertisements
