The Aggregation Framework is a tool that transforms and combines documents in a collection, returning results as arrays. It creates a pipeline for processing documents, including transformations, grouping, sorting, limiting, and skipping.
MongoDB aggregation operations collect and compute values from data records/documents, collect them, and execute operations such as total, average, minimum, and maximum. It works similarly to SQL's aggregate function, allowing for quick data translation, filtering, and analysis across numerous documents.
MongoDB provides three ways to perform aggregation:
MongoDB aggregation pipelines are made up of steps, each of which modifies a document. It is a multi-stage pipeline in which the documents are used as input to create the final set of documents.
In the MongoDB aggregation framework, each command acts on a stream of documents, then performs some type of transformation, and finally returns the transformation result. If it is the last operator in the pipeline, the result will be returned to the user. Otherwise, the result is passed along to the next operator as input.
The basic pipeline stages offer:
Map Reduce is used to aggregate results from massive amounts of data. Map reduction contains two primary functions: map to group all documents, and reduce to conduct operations on the grouped data.
MapReduce is a very powerful tool in the Aggregation Framework. It runs any type of JavaScript as a query language, allowing us to conduct advanced business logic. However, from a performance view, it is far too slow and should never be used with an active database.
Counting the number of documents or locating all distinct values in a document are two examples of when we utilize it when we need simple access to documents. It just gives access to the common aggregation process via the count(), distinct(), and estimatedDocumentCount() methods, limiting the pipeline's flexibility and capabilities.