Count occurrences of duplicate values
Asked Answered
G

1

7

How do I structure my MongooseJS/MongoDB query to get total duplicates/occurrences of a particular field value? Aka: The total documents with custID of some value for all custIDs

I can do this manually in command line:

db.tapwiser.find({"custID" : "12345"}, {}, {}).count();

Outputs: 1

db.tapwiser.find({"custID" : "6789"}, {}, {}).count();

Outputs: 4


I found this resource:

How to sum distinct values of a field in a MongoDB collection (utilizing mongoose)

But it requires that I specify the unique fields I want to sum.

In this case, I want to loop through all documents, sum the occurrences of each.

Gesticulatory answered 6/1, 2016 at 23:14 Comment(0)
H
9

All you need to do is $group your documents by custID and use the $sum accumulator operator to return "count" for each group.

db.tapwiser.aggregate(
    [ 
        { "$group":  { "_id": "$custID", "count": { "$sum": 1 } } }
    ],  function(err, results) {
            // Do something with the results
        }
)
Haiti answered 7/1, 2016 at 7:20 Comment(3)
Is there a way to filter out the items where count is 1, thus leaving only the duplicates in the output? Can't quite wrap my head around the aggregation framework and what it can and can't do.Curie
Hi @VioletGiraffe, To select on duplicates, you need to finish the aggregation pipeline with a $match stage i.e you need to add {$match: {"count": {"$gte": 2}}} after the $group stage. I think the aggregation framework is MongoDB swiss army knife, you can do many things like first order derivativeHaiti
Much appreciated!Curie

© 2022 - 2024 — McMap. All rights reserved.