Get number of user posts and his comments count

Viewed 94

in my DB collection I have the following documents structure: { owner: ObjectId, counter: number }. As a side note, this is a state I've reached to using aggregate on my real documents. So basically owner is of ObjectId type, and counter fields reporesents a counter.

My target is, given an input x of type ObjectId, to count how many times x presents as an owner (this would be easily achieved by using the $count pipe stage), but also overall sum of counters.

For example, consider the following documents and given id input owner = 1:

{ owner: 1, counter: 20 }, { owner: 2, counter: 10 }, { owner: 3, counter: 2 }, { owner: 1, counter: 5 },

Then, my target is to get a final aggregation result of: { numberOfBeingOwner: 2, overallSumCounter: 20+10+2+5 }

How can I do it using the aggregation pipeline?

0 Answers
Related