Transformation Pipeline Stages
  • 3 Minutes to read
  • Dark
    Light

Transformation Pipeline Stages

  • Dark
    Light

Article summary

On this page, you will find a comprehensive list of transformation pipeline stages and their options.

Transformation Pipeline Stages

Stage NameDescription
$addFieldsAdds new fields to documents.
$bucketGroups documents into specified buckets based on an expression and boundaries. Options available.
$bucketAutoAutomatically distributes documents into a specified number of even buckets. Options available.
$changeStreamReturns a Change Stream cursor; must be the first stage in the pipeline.
$collStatsProvides statistics about a collection or view.
$countCounts the documents at this pipeline stage.
$currentOpDisplays active and/or dormant operations. Requires the db.aggregate() method. Options available.
$facetExecutes multiple aggregation pipelines in one stage, enabling multi-faceted aggregations.
$geoNearOrders documents based on geospatial proximity, incorporating $match, $sort, and $limit. Options available.
$graphLookupConducts a recursive search, appending results to the output document as an array. Options available.
$groupGroups documents by an expression, applying accumulator expressions to each group.
$indexStatsReports usage statistics for each index in the collection.
$limitPasses the first n documents unmodified to the pipeline.
$listLocalSessionsLists active sessions on the connected mongos or mongod instance.
$listSessionsLists sessions propagated to the system.sessions collection.
$lookupPerforms a left outer join with another collection in the same database. Options available.
$matchFilters documents, passing only matches to the next stage.
$mergeWrites aggregation results to a collection; must be the last stage in the pipeline. Options available.
$outWrites aggregation results to a collection; must be the last stage. Options available.
$planCacheStatsProvides plan cache data for a collection.
$projectReshapes each document, adding or removing fields.
$redactReshapes documents based on their content, allowing field level redaction.
$replaceRootReplaces a document with a specified embedded one, promoting the embedded document.
$replaceWithAlias for $replaceRoot. Replaces a document with a specified embedded one.
$sampleRandomly selects a specified number of documents. Options available.
$searchExecutes a full-text search.
$setAlias for $addFields. Adds new fields to documents.
$skipSkips the first n documents, passing the rest unmodified.
$sortReorders documents based on a specified sort key.
$sortByCountGroups documents by an expression's value and counts them.
$unionWithCombines results from two collections into one. Options available.
$unsetRemoves specified fields from documents.
$unwindDeconstructs an array field, outputting a document for each element. Options available.

Stages Options

StageOption
$bucketgroupBy: An expression to group by.
boundaries: An array specifying the boundaries to group documents.
default: A literal that specifies the _id of an additional group for all documents that do not match any boundary.
$bucketAutogroupBy: An expression to group by.
buckets: The number of buckets to create.
$currentOpallUsers: A boolean indicating if operations from all users should be returned.
localOps: A boolean indicating if only operations on the local mongod instance should be returned.
$geoNearnear: The geospatial point to calculate distances from.
distanceField: The field that will contain the calculated distance.
spherical: A boolean specifying whether to treat the near point as a sphere.
maxDistance: The maximum distance from the near point that the documents can be.
$graphLookupfrom: The target collection for the recursive search.
startWith: The expression that specifies the value of the connectFromField with which to start the recursive search.
connectFromField: The field whose value startWith references.
connectToField: The field in the documents of the from collection that is equal to the connectFromField.
as: The array field added to each output document that contains the search results.
$lookupfrom: The target collection.
localField: The field from the input documents.
foreignField: The field from the documents of the from collection.
as: The name of the new array field to add to the input documents.
$mergeinto: The target collection.
on: The fields to match on.
whenMatched: The action to take when a matching document exists.
whenNotMatched: The action to take when no matching document exists.
$outcollection: The target collection.
$samplesize: The number of documents to randomly select.
$unionWithcoll: The name of the collection to union with.
pipeline: An optional aggregation pipeline to apply to the specified collection before performing the union.
$unwindpath: The path to the array field to unwind.
includeArrayIndex: The name of a new field to hold the array index of the element.
preserveNullAndEmptyArrays: A boolean indicating whether to include as an output document those documents that have an empty array or missing array field.

Was this article helpful?