$facet

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

Reference group: Pipeline stages

What it does

Runs multiple sub-pipelines over the same input.

Syntax

{$facet: { name: [pipeline], ... }}

Use case

Combine counts and result facets.

Important notes

Mind memory and output-document size limits.

$facet: practical example
{ $facet: { byStatus: [{ $group: { _id: "$status", count: { $sum: 1 } } }], sample: [{ $limit: 5 }] } }
💡 Explanation Runs multiple sub-pipelines over the same input. When to use it Combine counts and result facets. Caution Mind memory and output-document size limits.
Key Takeaways
  • Runs multiple sub-pipelines over the same input.
  • Typical use: Combine counts and result facets.