Pipeline Practice

MongoDB $facet

Run several independent sub-pipelines on the same input.

Each named branch receives the same documents and returns its results as an array. Use it when one response needs several coordinated summaries.

Stages are the top-level steps in a pipeline. Each stage receives documents, changes the stream in one specific way, and passes its output to the next stage.

Course use: Exercise 11 and Hard Mode 13.

Example pipeline syntax

{ $facet: {
  byChannel: [{ $group: { _id: "$channel" } }],
  byRegion: [{ $group: { _id: "$region" } }]
} }