The MongoDB aggregation pipeline processes documents through a sequence of stages. Each stage transforms the data and passes results to the next stage.
Key stages: $match (filter documents), $group (aggregate by field), $project (reshape documents), $sort, $limit, $unwind (deconstruct arrays), $lookup (join collections).
Example: db.orders.aggregate([ $match({ status: 'completed' }), $group({ _id: '$customerId', totalSpent: { $sum: '$amount' } }), $sort({ totalSpent: -1 }) ]).
$lookup performs left outer joins between collections. $addFields creates computed fields. $bucket groups values into ranges. $facet runs multiple pipelines in parallel.
Performance: create indexes on $match fields. Use $project early to reduce document size. Avoid $unwind on large arrays when possible.
Atlas Search adds full-text search capabilities to the aggregation pipeline. $unionWith combines results from multiple collections.