$sortByCount

Reference type:Cheat sheetFormat:Syntax + example + use case

Reference group: Pipeline stages

What it does

Groups by expression, counts, then sorts descending by count.

Syntax

{$sortByCount: expression}

Use case

Find popular categories.

$sortByCount: practical example
{ $sortByCount: "$category" }
💡 Explanation Groups by expression, counts, then sorts descending by count. When to use it Find popular categories.
Key Takeaways
  • Groups by expression, counts, then sorts descending by count.
  • Typical use: Find popular categories.