An aggregation pipeline is composed of different stages. These stages are declared in an array and executed sequentially, the output of every stage being the input of the next one.
The $out stage has to be the final stage in an aggregation pipeline, outputting data to an output collection by replacing or adding to the existing documents.
- $group: This is most commonly used to group by identifier expression and to apply the accumulator expression. It outputs one document per distinct group.
- $project: This is used for document transformation and outputs one document per input document.
- $match: This is used for filtering documents from input based on criteria.
- $lookup: This is used for filtering documents from input. Input can be documents from another collection in the same database selected by an outer left join.
- $out: This outputs the documents in this pipeline stage to an output collection by replacing or adding to the documents that already exist in the collection.
- $limit: This limits the number of documents passed on to the next aggregation phase based on criteria.
- $count: This returns a count of the number of documents at this stage of the pipeline.
- $skip: This skips a number of documents from passing on to the next stage of the pipeline.
- $sort: This sorts the documents based on criteria.
- $redact: As a combination of project and match, this will redact the selected fields from each document passing them on to the next stage of the pipeline.
- $unwind: This transforms an array of n elements to n documents each, with one element of the array passing them on to the next stage of the pipeline.
- $collStats: This returns statistics regarding the view or collection.
- $indexStats: This returns statistics regarding the indexes of the collection.
- $sample: This randomly selects a specified number of documents from input.
- $facet: This combines multiple aggregation pipelines within a single stage.
- $bucket: This splits documents into buckets based on selection criteria and bucket boundaries.
- $bucketAuto: This splits documents into buckets based on selection criteria and attempts to evenly distribute documents amongst buckets.
- $sortByCount: This groups incoming documents based on value of an expression and computes the count of documents in each bucket.
- $addFields: This adds new fields to documents and outputs the same number of documents as input, with the added fields.
- $replaceRoot: This replaces all existing fields of the input document (including the standard _id field) with the specified fields.
- $geoNear: This returns an ordered list of documents based on the proximity to a specified field. The output documents include a computed distance field.
- $graphLookup: This recursively searches in a collection and adds an array field with the results of the search in each output document.