Chapter 4 Core Aggregation facets

Hi,
So we have the $sortByCount stage in the aggregation pipeline. This will group according to a facet or field and will also count and sort accordingly. I just wanted to know, what advantages this provides over $group since $group also provides a functionality closely similar to this.

Thanks,
Apurva.

Hi,

Indeed $sortByCount is equivalent to $group (with $sum) followed by a $sort:

https://docs.mongodb.com/manual/reference/operator/aggregation/sortByCount/#behavior

José Carlos

1 Like

With a slight difference,
in $sortByCount the default sort parameter is count and the default order is descending,
while in the $group stage you have the flexibility of sorting by whichever parameter you want in whichever order.

Thanks,
Apurva.

Good point!

José Carlos