In MongoDB the $sortByCount
aggregation pipeline stage groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.
Each group is output in its own document, which consists of two fields:
- an
_id
field containing the distinct grouping value, and - a
count
field containing the number of documents belonging to that grouping.
The documents are sorted by count
in descending order.