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
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
- $project ? Selects only the array field to process
- $unwind ? Deconstructs the array into separate documents for each element
- $group ? Groups by document ID and array value, counting occurrences
- $match ? Filters for counts greater than 1 (duplicates)
- $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.
Advertisements
