Try Studio 3T for Free
  1. Knowledge Base
  2. MongoDB Aggregation Framework
  3. Supported MongoDB Aggregation Operators and Stages

Supported MongoDB Aggregation Operators and Stages

Aggregation Editor is Studio 3T’s stage-by-stage aggregation query builder. Learn more about the feature.

All descriptions have been taken from MongoDB’s documentation. You can view the full list of MongoDB aggregation stages here.


Adds new fields to documents. Similar to $project, $addFields reshapes each document in the stream; specifically, by adding new fields to output documents that contain both the existing fields from the input documents and the newly added fields.

{ $addFields: { <newField>: <expression>, ... } }


Categorizes incoming documents into groups, called buckets, based on a specified expression and bucket boundaries.

  $bucket: {
      groupBy: <expression>,
      boundaries: [ <lowerbound1>, <lowerbound2>, ... ],
      default: <literal>,
      output: {
         <output1>: { <$accumulator expression> },
         <outputN>: { <$accumulator expression> }


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.

  $bucketAuto: {
      groupBy: <expression>,
      buckets: <number>,
      output: {
         <output1>: { <$accumulator expression> },
      granularity: <string>


Returns statistics regarding a collection or view.

      latencyStats: { histograms: <boolean> },
      storageStats: { scale: <number> },
      count: {}


Returns a count of the number of documents at this stage of the aggregation pipeline.

{ $count: <string> }


Returns a stream of documents containing information on active and/or dormant operations as well as inactive sessions that are holding locks as part of a transaction. The stage returns a document for each operation or session.

{ $currentOp: { allUsers: <boolean>, idleConnections: <boolean>, idleCursors: <boolean>, idleSessions: <boolean>, localOps: <boolean> } }


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.

{ $facet:
      <outputField1>: [ <stage1>, <stage2>, ... ],
      <outputField2>: [ <stage1>, <stage2>, ... ],



Returns an ordered stream of documents based on the proximity to a geospatial point. Incorporates the functionality of $match, $sort, and $limit for geospatial data. The output documents include an additional distance field and can include a location identifier field.

{ $geoNear: { <geoNear options> } }


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.

   $graphLookup: {
      from: <collection>,
      startWith: <expression>,
      connectFromField: <string>,
      connectToField: <string>,
      as: <string>,
      maxDepth: <number>,
      depthField: <string>,
      restrictSearchWithMatch: <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.

      _id: <expression>, // Group By Expression
      <field1>: { <accumulator1> : <expression1> },


Returns statistics regarding the use of each index for the collection.

The $indexStats stage takes an empty document and has the following syntax:

{ $indexStats: { } }

View the full MongoDB documentation.


Limits the number of documents passed to the next stage in the pipeline.

{ $limit: <positive integer> }


Lists all sessions that have been active long enough to propagate to the system.sessions collection.

{ $listSessions: <document> }


Lists the sessions cached in memory by the mongod or mongos instance.

{ $listLocalSessions: <document> }


Performs a left outer join to another collection in the same database to filter in documents from the “joined” collection for processing.

Equality Match syntax

       from: <collection to join>,
       localField: <field from the input documents>,
       foreignField: <field from the documents of the "from" collection>,
       as: <output array field>

Join Conditions and Uncorrelated Sub-queries syntax

       from: <collection to join>,
       let: { <var_1>: <expression>, …, <var_n>: <expression> },
       pipeline: [ <pipeline to execute on the collection to join> ],
       as: <output array field>


Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

{ $match: { <query> } }


Writes the resulting documents of the aggregation pipeline to a collection. To use the $out stage, it must be the last stage in the pipeline.

{ $out: "<output-collection>" }


Reshapes each document in the stream, such as by adding new fields or removing existing fields. For each input document, outputs one document.

{ $project: { <specification(s)> } }


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 $project and $match. Can be used to implement field level redaction. For each input document, outputs either one or zero documents.

{ $redact: <expression> }


Replaces a document with the specified embedded document. The operation replaces all existing fields in the input document, including the _id field. Specify a document embedded in the input document to promote the embedded document to the top level.

{ $replaceRoot: { newRoot: <replacementDocument> } }


Randomly selects the specified number of documents from its input.

{ $sample: { size: <positive integer> } }


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).

{ $skip: <positive integer> }


Reorders the document stream by a specified sort key. Only the order changes; the documents remain unmodified. For each input document, outputs one document.

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }


Groups incoming documents based on the value of a specified expression, then computes the count of documents in each distinct group.

{ $sortByCount:  <expression> }


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 ndocuments where n is the number of array elements and can be zero for an empty array.

      path: <field path>,
      includeArrayIndex: <string>,
      preserveNullAndEmptyArrays: <boolean>


Updated on October 6, 2020

Was this article helpful?

Related Articles


  1. There are aggregation stages missing – why? Why can I not choose a stage that doesnt exist in the dropdown menu? forr example dateFromString

Leave a Comment