I have a compilation of records that contain various feedback for different products. The structure is as follows:
{
{
item: "item_1"
rating: "neutral"
comment: "some comment"
},
{
item: "item_2"
rating: "good"
comment: "some comment"
},
{
item: "item_1"
rating: "good"
comment: "some comment"
},
{
item: "item_1"
rating: "bad"
comment: "some comment"
},
{
item: "item_3"
rating: "good"
comment: "some comment"
},
}
My goal is to determine the frequency of each rating for every item.
Therefore, the desired output should be something like this:
{
{
item: "item_1"
good: 12
neutral: 10
bad: 67
},
{
item: "item_2"
good: 2
neutral: 45
bad: 8
},
{
item: "item_3"
good: 1
neutral: 31
bad: 10
}
}
This is what I have attempted so far
db.collection(collectionName).aggregate(
[
{
$group:
{
_id: "$item",
good_count: {$sum: {$eq: ["$rating", "Good"]}},
neutral_count:{$sum: {$eq: ["$rating", "Neutral"]}},
bad_count:{$sum: {$eq: ["$rating", "Bad"]}},
}
}
]
)
The format of the output appears correct, but the counts are consistently 0.
I am curious about the appropriate method to total the occurrences based on the distinct values within the same field?
Thank you!