MongoDB Query Language (MQL) Aggregation Operators

$addFields

The `$addFields` stage in the aggregation pipeline is used to add new fields to documents. It can also be used to reset the values of existing fields. This stage is particularly useful when you need to create new fields based on existing data or modify existing fields within your documents.

$bucket

The `$bucket` stage in an aggregation pipeline groups input documents into buckets based on specified boundaries. This is especially useful for creating histograms or categorizing data into ranges.

$changeStream

The `$changeStream` stage returns a change stream cursor that tracks changes to a collection. Change streams allow applications to access real-time data changes without the complexity and risk of tailing the oplog.

$collStats

The `$collStats` stage in the aggregation pipeline is used to return statistics about a collection. This stage provides detailed information that can help with database optimization and monitoring.

$convert

The $convert operator converts an expression into a value of the specified type.

$count

The `$count` stage returns a count of the number of documents at this stage of the aggregation pipeline. It outputs a document with a single field containing the count.

$densify

The `$densify` stage in an aggregation pipeline is used to fill in missing data points within a sequence of values. It helps in creating a more complete dataset by generating missing values based on a specified field, range, and step. This is useful in scenarios like time-series data analysis, where gaps in data points need to be filled to ensure accurate analysis.

$documents

The `$documents` aggregation pipeline stage is used to create a pipeline from a set of provided documents. This stage is particularly useful when you want to process specific documents without querying a collection.

$facet

The `$facet` stage allows for multiple parallel aggregations to be executed within a single pipeline stage. It's useful for performing multiple analyses on the same dataset in a single query.

$fill

The `$fill` stage is used to fill missing or null values in documents within the aggregation pipeline. It provides various methods to populate missing data, including using static values, linear interpolation, or values from previous/next documents.

$geoNear

The `$geoNear` aggregation stage calculates distances between a specified point and the location field in each document, sorts the documents by distance, and can optionally limit results by distance.

$group

The `$group` aggregation stage groups documents by specified identifier expressions and applies accumulator expressions to create computed fields for each group. This stage is essential for data aggregation and summarization operations.

$indexStats

The `$indexStats` aggregation stage returns usage statistics for each index in the collection. This stage is useful for analyzing index performance, identifying unused indexes, and optimizing query performance.

$isNumber

The `$isNumber` operator returns true if the input expression is a numerical type. The `$isNumber` operator returns false for an expression of any other type.

$lookup

The `$lookup` stage in the Aggregation Framework is used to perform left outer joins with other collections. It allows you to combine documents from different collections based on a specified condition, enriching documents with related data.

$match

The `$match` stage in the aggregation pipeline is used to filter documents that match a specified condition. It's similar to the `find` operation but is used within the aggregation pipeline to narrow down the documents that pass through to the next stage.

$merge

The `$merge` stage in an aggregation pipeline is used to write the results of the aggregation query into a specified collection. This stage combines data transformation and data persistence in a single operation.

$out

The `$out` stage in an aggregation pipeline allows you to write the resulting documents of the pipeline into a specified collection. It is commonly used to save the output of complex aggregation operations for further use or analysis.

$redact

The `$redact` stage in aggregation pipeline is used to filter fields of the documents in a collection dynamically based on access rights or other conditions. It processes each document and removes or retains fields based on the specified logic.

$replaceWith

The `$replaceWith` aggregation stage operator is used to replace the input document with the specified document. It transforms documents from one structure to another or replaces them entirely with new fields and values.

$sample

The `$sample` stage is used in aggregation pipelines to randomly select a specified number of documents from a collection. The `$sample` command is useful during testing, data analysis, and generating random subsets of data for machine learning.

$set

The `$set` operator updates an existing field or creates a new field with the specified value if it does not exist. One or more fields listed are updated or created. The dot notation is used to update or create nested objects.

$skip

The `$skip` stage in the aggregation pipeline is used to skip a specified number of documents from the input and pass the remaining documents to the next stage in the pipeline. This stage is useful for implementing pagination in queries.

$sort

The `$sort` stage in the aggregation pipeline is used to order the documents in the pipeline by a specified field or fields. This stage helps you sort data, like arranging sales by amount or events by date.

$sortByCount

The $sortByCount stage in the aggregation pipeline is used to group documents by a specified expression and then sort the count of documents in each group in descending order. The `$sortByCount` stage is useful for quickly identifying the most common values within a dataset.

$toBool

The `$toBool` operator converts an expression into a Boolean value. Boolean values are returned as is without a conversion. Nonzero numeric values are converted to true while Decimal, Long, Double or Int values of 0 are converted to false. All other data types are converted to true.

$toDate

The `$toDate` operator converts a specified value into a date type.

$toDecimal

The `$toDecimal` operator converts an input expression into a Decimal value. Long, Double or Int values are simply converted to a Decimal data type, while Decimal values are returned as is. A boolean value of true is converted to 1, while a boolean false is converted to 0. Lastly, ISODates are converted to a Decimal value corresponding to the number of milliseconds since January 1st, 1970 represented by the ISODate value.

$toDouble

The `$toDouble` operator converts a specified value into a Double value.

$toInt

The `$toInt` operator converts a specified value into an integer value.

$toLong

The `$toLong` operator converts a specified value into a Long value.

$toObjectId

The `$toObjectId` operator converts a specified value into an ObjectId.

$toString

The `$toString` operator simply returns the value of the specified expression as a String.

$unset

The $unset stage in the aggregation pipeline is used to remove specified fields from documents. This can be particularly useful when you need to exclude certain fields from the results of an aggregation query for reasons such as privacy, reducing payload size, or simply cleaning up the output.

$unwind

The `$unwind` stage in the aggregation framework is used to deconstruct an array field from the input documents to output a document for each element. This is particularly useful for normalizing data stored in arrays and for performing operations on each element separately.