I am trying to count how many times does a particular value occur in a collection.
{
_id:1,
field1: value,
field2: A,
}
{
_id:2,
field1: value,
field2: A,
}
{
_id:3,
field1: value,
field2: C,
}
{
_id:4,
field1: value,
field2: B,
}
what I want is to count how many times A occurs, B occurs and C occurs and return the count.
The output I want
{
A: 2,
B: 1,
C: 1,
}
3
Answers
You can use
$facet
in an aggregate pipeline like this:$facet
create "three ways" where in each one filter the values by desired key (A, B or C).$project
stage you can get the$size
of the matched values.Example here
This is typical use case for
$group
stage in Aggregation Pipeline. You can do it like this:$group
– to group all the documents by field2$sum
– to count the number of documents for each value of field2Working example
Leverage the
$arrayToObject
operator and a final$replaceWith
pipeline to get the desired result. You would need to run the following aggregate pipeline:Mongo Playground