The aggregation operation in MongoDB is an effective way to process data and return computed results. Aggregation operations group values from multiple documents and can perform a variety of operations on this grouped data to return a single result. MongoDB provides three ways to perform aggregations: the aggregation pipeline, the map-reduce method, and single-purpose aggregation operations.
Aggregation Pipeline
Aggregation pipelines in MongoDB use pipeline operators to process and transform your documents into grouped stages. Each document moves through these pipeline stages in order. The aggregation pipeline is a powerful framework that provides capabilities similar to SQL GROUP BY queries.
Pipeline operators include $match, $group, $sort, $project, $limit, and many others. $match filters documents, $group groups values from multiple documents together, $sort sorts the documents, $project reshuffles each document, $limit limits the number of documents to move on to the next step.
Map-Reduce Operations
The map-reduce method is a flexible way to perform aggregations, especially when dealing with large volumes of data. Map-Reduce is a data processing model for condensing large volumes of data into useful aggregated results. In MongoDB, map-reduce operates on a collection of documents and returns a collection result.
The map-reduce process in MongoDB first maps data to output key-value pairs and then reduces all pairs with the same key. Mapping and reducing are custom JavaScript operations that MongoDB performs on the collection.
Single-Purpose Aggregation Operations
MongoDB also provides some single-purpose aggregation operations, such as counting the number of documents, calculating the sum of values, and finding minimum and maximum values in a collection.
Examples of Aggregation Operations
Let's consider some examples of aggregation operations in MongoDB. Suppose we have an 'orders' collection with documents that contain 'customer_id' and 'amount'.
To find the total sum of 'amount' for each 'customer_id', we can use the aggregation pipeline with the $group: operator
db.orders.aggregate([ { $group : { _id : "$customer_id", total : { $sum : "$amount" } } } ])
This code groups documents by 'customer_id' and adds the 'amount' values for each group.
To find the total sum of 'amount' for all documents, we can use the map-reduce method:
db.orders.mapReduce( function() { emit(null, this.amount); }, function(key, values) { return Array.sum(values); }, { out: "order_totals" } )
This code outputs the 'amount' of each document with a null key and then sums all the values output.
To count the number of documents in the collection, we can use the single-purpose aggregation operation 'count':
db.orders.count()
This code returns the number of documents in the 'orders' collection.
Aggregation operations in MongoDB are a powerful and flexible way to work with data. They allow you to process and transform your documents in multiple ways to get the results you want.