$sort

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

Reference group: Pipeline stages

What it does

Orders documents.

Syntax

{$sort: { field: 1 or -1 }}

Use case

Newest-first views and deterministic pagination.

Important notes

Use a unique tie-breaker when stable order matters.

$sort: practical example
{ $sort: { createdAt: -1, _id: -1 } }
💡 Explanation Orders documents. When to use it Newest-first views and deterministic pagination. Caution Use a unique tie-breaker when stable order matters.
Key Takeaways
  • Orders documents.
  • Typical use: Newest-first views and deterministic pagination.