I have the following in an aggregation:
[
{ _id: 610b678502500b0646923801, feeling: 'dislike' },
{ _id: 610b678502500b0646923629, feeling: 'like' },
{ _id: 610b67a602500b064693a667, feeling: 'love' },
{ _id: 610b678d02500b06469290fd, feeling: 'like' },
{ _id: 610b678502500b06469238f3, feeling: 'love' },
{ _id: 610b678502500b06469237ed, feeling: 'love' },
{ _id: 610b678502500b064692389e, feeling: 'like' },
{ _id: 610b678502500b0646923bd8, feeling: 'love' },
{ _id: 610b678502500b06469237e0, feeling: 'love' },
{ _id: 610b678502500b0646923674, feeling: 'love' },
{ _id: 610b680b02500b0646981b3a, feeling: 'dislike' },
{ _id: 610b678702500b0646925096, feeling: 'love' },
{ _id: 610b678502500b0646923810, feeling: 'like' },
{ _id: 610b678d02500b06469292fb, feeling: 'dislike' },
{ _id: 610b678502500b06469238b4, feeling: 'like' }
...
]
I want to group by _id, and show the number of items for each feeling. The expected results would be:
[
{
_id: 610b678502500b0646923801,
love: 2,
like: 4,
dislike: 6
},
{
_id: 610b678502500b06469237ed,
love: 8,
like: 2,
dislike: 5
}
...
]
2
Answers
Perhaps something like this:
Explained:
— in your aggregation you need to continue from here —
Playground
Query
Playmongo