Overview
In this guide, you can learn how to create an aggregation pipeline and pipeline stages by using methods in the MongoDB Node.js driver.
Build an Aggregation Pipeline
You can use the Node.js driver to build an aggregation pipeline by creating a pipeline variable or passing aggregation stages directly into the aggregation method. See the following examples to learn more about each of these approaches.
// Defines the aggregation pipeline const pipeline = [ { $match: { ... } }, { $group: { ... } } ]; // Executes the aggregation pipeline const results = await collection.aggregate(pipeline);
// Defines and executes the aggregation pipeline const results = await collection.aggregate([ { $match: { ... } }, { $group: { ... } } ]);
Aggregation Stage Methods
The following table lists the stages in the aggregation pipeline. To learn more about an aggregation stage and see a code example in a Node.js application, follow the link from the stage name to its reference page in the MongoDB Server manual.
Stage | Description |
---|---|
Adds new fields to documents. Outputs documents that contain both the existing fields from the input documents and the newly added fields.
| |
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 change stream cursor for the collection. Must be the first stage in the pipeline.
| |
Splits large change stream events that exceed 16 MB into smaller fragments returned in a change stream cursor. Must be the last stage in the pipeline.
| |
Returns statistics regarding a collection or view. | |
Returns a count of the number of documents at this stage of the aggregation pipeline. | |
Returns a stream of documents containing information on active and dormant operations and any inactive sessions that are holding locks as part of a transaction. | |
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. | |
Returns documents in order of nearest to farthest from a specified point. This method adds a field to output documents that contains the distance from the specified point. | |
Performs a recursive search on a collection. This method adds a new array field to each output document that contains the traversal results of the recursive search for that document. | |
Groups input documents by a specified identifier expression and applies the accumulator expressions, if specified, to each group. Consumes all input documents and outputs one document per each distinct group. The output documents contain only the identifier field and, if specified, accumulated fields. | |
Returns statistics regarding the use of each index for the collection. | |
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). | |
Lists sampled queries for all collections or a specific collection. Only available for collections with Queryable Encryption enabled. | |
Returns information about existing Atlas Search indexes on a specified collection. | |
Performs a left outer join to another collection in the same database to filter in documents from the "joined" collection for processing. | |
Filters the document stream to allow only matching documents to pass unmodified into the next pipeline stage. For each input document, outputs either one document (a match) or zero documents (no match). | |
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 this stage, it must be the last stage in the pipeline. | |
Writes the resulting documents of the aggregation pipeline to a collection. To use this stage, it must be the last stage in the pipeline. | |
Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document. | |
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 The | |
Replaces a document with the specified embedded document.
The operation replaces all existing fields in the input document, including
the The | |
Randomly selects the specified number of documents from its input. | |
Performs a full-text search of the field or fields in an Atlas collection. This stage is available only for MongoDB Atlas clusters, and is not available for self-managed deployments. To learn more, see Atlas Search Aggregation Pipeline Stages in the Atlas documentation. | |
Returns different types of metadata result documents for the Atlas Search query against an Atlas collection. This stage is available only for MongoDB Atlas clusters, and is not available for self-managed deployments. To learn more, see Atlas Search Aggregation Pipeline Stages in the Atlas documentation. | |
Adds new fields to documents. Like the | |
Groups documents into windows and applies one or more operators to the documents in each window. | |
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. 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. | |
Combines pipeline results from two collections into a single result set. | |
Removes/excludes fields from documents.
| |
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. n can be zero for an empty array. | |
Performs an ANN or ENN search on a vector in the specified field of an Atlas collection. This stage is available only for MongoDB Atlas clusters, and is not available for self-managed deployments. To learn more, see Atlas Vector Search. |
API Documentation
To learn more about assembling an aggregation pipeline, see Aggregation Pipeline in the MongoDB Server manual.
To learn more about creating pipeline stages, see Aggregation Stages in the MongoDB Server manual.
For more information about the methods and classes used on this page, see the following API documentation: