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
Calculate frequency of duplicate names from NAME field using MongoDB aggregate?
To calculate the frequency of duplicate names in MongoDB, use the $group stage in the aggregation pipeline to group documents by the Name field and count occurrences with $sum.
Syntax
db.collection.aggregate([
{ $group: { "_id": "$fieldName", count: { $sum: 1 } } }
]);
Create Sample Data
db.demo635.insertMany([
{Name: "Chris"},
{Name: "David"},
{Name: "David"},
{Name: "Chris"},
{Name: "Bob"},
{Name: "Chris"}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5e9c10f06c954c74be91e6cc"),
ObjectId("5e9c10f46c954c74be91e6cd"),
ObjectId("5e9c10f66c954c74be91e6ce"),
ObjectId("5e9c10f86c954c74be91e6cf"),
ObjectId("5e9c10fb6c954c74be91e6d0"),
ObjectId("5e9c10fc6c954c74be91e6d1")
]
}
Display Sample Data
db.demo635.find();
{ "_id" : ObjectId("5e9c10f06c954c74be91e6cc"), "Name" : "Chris" }
{ "_id" : ObjectId("5e9c10f46c954c74be91e6cd"), "Name" : "David" }
{ "_id" : ObjectId("5e9c10f66c954c74be91e6ce"), "Name" : "David" }
{ "_id" : ObjectId("5e9c10f86c954c74be91e6cf"), "Name" : "Chris" }
{ "_id" : ObjectId("5e9c10fb6c954c74be91e6d0"), "Name" : "Bob" }
{ "_id" : ObjectId("5e9c10fc6c954c74be91e6d1"), "Name" : "Chris" }
Calculate Name Frequency
Group documents by Name field and count occurrences using the aggregation framework ?
db.demo635.aggregate([
{ $group: { "_id": "$Name", TotalFrequency: { $sum: 1 } } }
]);
{ "_id" : "David", "TotalFrequency" : 2 }
{ "_id" : "Bob", "TotalFrequency" : 1 }
{ "_id" : "Chris", "TotalFrequency" : 3 }
Key Points
- The
$groupstage groups documents by the specified field ($Name). -
$sum: 1increments the counter for each document in the group. - The
_idfield in the output contains the grouped field value.
Conclusion
Use MongoDB's $group aggregation stage with $sum: 1 to efficiently calculate the frequency of duplicate values in any field. This approach groups identical values and counts their occurrences in a single query.
Advertisements
