In the db.collection.aggregate() and db.aggregate() methods, pipeline stages appear in an array. Documents pass through the stages in sequence. In the Atlas UI, arrange pipeline stages with the aggregation pipeline builder.
db.collection.aggregate() Stages
All stages except the $out, $merge, $geoNear, $changeStream, and $changeStreamSplitLargeEvent stages can appear multiple times in a pipeline.
Note
For details on a specific command, including syntax and examples, click on the link to the command's reference page.
db.collection.aggregate( [ { <stage> }, ... ] )
Core Stages
Stages that filter, reshape, group, and combine documents in the aggregation pipeline.
Stage | Description |
|---|---|
Adds new fields to documents. Similar to
| |
Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries. | |
Categorizes incoming documents into a specific number of groups, called buckets, based on a specified expression. Bucket boundaries are automatically determined in an attempt to evenly distribute the documents into the specified number of buckets. | |
Returns a count of the number of documents at this stage of the aggregation pipeline. Distinct from the | |
Creates new documents in a sequence of documents where certain values in a field are missing. | |
Returns literal documents from input expressions. | |
Processes multiple aggregation pipelines within a single stage on the same set of input documents. Enables the creation of multi-faceted aggregations capable of characterizing data across multiple dimensions, or facets, in a single stage. | |
Populates | |
Performs a recursive search on a collection. To each output document, adds a new array field that contains the traversal results of the recursive search for that document. | |
Groups input documents by a specified identifier expression and applies the accumulator expression(s), if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents only contain the identifier field and, if specified, accumulated fields. | |
Passes the first n documents unmodified to the pipeline where n is the specified limit. For each input document, outputs either one document (for the first n documents) or zero documents (after the first n documents). | |
Performs a left outer join to another collection or view in the same database to filter in documents from the "joined" collection or view for processing. | |
Filters the document stream to allow only matching documents to pass unmodified into the next pipeline stage. | |
Writes the resulting documents of the aggregation pipeline to a collection. The stage can incorporate (insert new documents, merge documents, replace documents, keep existing documents, fail the operation, process documents with a custom update pipeline) the results into an output collection. To use the | |
Writes the resulting documents of the aggregation pipeline to a collection. To use the | |
Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document. See also | |
Reshapes each document in the stream by restricting the content for each document based on information stored in the documents themselves. Incorporates the functionality of | |
Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the
| |
Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the
| |
Randomly selects the specified number of documents from its input. | |
Adds new fields to documents. Similar to
| |
Groups documents into windows and applies one or more operators to the documents in each window. New in version 5.0. | |
Skips the first n documents where n is the specified skip number and passes the remaining documents unmodified to the pipeline. For each input document, outputs either zero documents (for the first n documents) or one document (if after the first n documents). | |
Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document. | |
Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group. | |
Performs a union of two collections; i.e. combines pipeline results from two collections or views into a single result set. | |
Deconstructs an array field from the input documents to output a document for each element. Each output document replaces the array with an element value. For each input document, outputs n documents where n is the number of array elements and can be zero for an empty array. |
Language Extensions
Stages that extend the aggregation language with specialized capabilities, such as Atlas Search and change streams.
Stage | Description |
|---|---|
Returns a Change Stream cursor for the collection. This stage can only occur once in an aggregation pipeline and it must occur as the first stage. | |
Splits large change stream events that exceed 16 MB into smaller fragments returned in a change stream cursor. You can only use | |
Combines the results of input pipelines that rank documents. | |
Performs a full-text search of the field or fields in a collection. To learn more, see MongoDB Search Aggregation Pipeline Stages. | |
Returns different types of metadata result documents for the MongoDB Search query against an Atlas collection. To learn more, see MongoDB Search Aggregation Pipeline Stages. | |
Performs an ANN or ENN search on a vector in the specified field of an Atlas collection.
New in version 7.0.2. |
Metadata Sources
Stages that return metadata about the deployment, such as collection statistics, index information, and active operations.
Stage | Description |
|---|---|
Returns statistics regarding a collection or view. | |
Returns information on active and/or dormant operations for the MongoDB deployment. Uses the | |
Returns statistics regarding the use of each index for the collection. | |
Retrieves information for collections in a cluster, including names and creation options. | |
Lists all active sessions recently in use on the currently connected Uses the | |
Lists sampled queries for all collections or a specific collection. | |
Returns information about existing MongoDB Search indexes on a specified collection or view. | |
Lists all sessions that have been active long enough to propagate to the | |
Returns plan cache information for a collection. | |
Returns query settings previously added with New in version 8.0. | |
Returns runtime statistics for recorded queries. WARNING: The | |
Returns information on the distribution of data in sharded collections. Uses the New in version 6.0.3. |
To learn about expressions that you can use in pipeline stages, see Expressions.
Stages Available for Updates
Use the aggregation pipeline for updates in:
Command | mongosh Methods |
|---|---|
For updates, the pipeline supports these stages:
$addFieldsand its alias$set$replaceRootand its alias$replaceWith