Advanced Usage of Aggregation Pipelines in MongoDB
Introduction
The aggregation pipeline is a powerful framework for performing data aggregation operations in MongoDB. It consists of a series of stages, each of which transforms the documents as they pass through the pipeline. This tutorial covers advanced usage of the aggregation pipeline, including common stages and practical examples.
Common Aggregation Stages
Here are some common aggregation stages and their purposes:
$match
: Filters documents to pass only those that match the specified condition.$group
: Groups documents by a specified expression and applies an accumulator to each group.$project
: Reshapes each document by including, excluding, or adding fields.$sort
: Sorts documents by a specified field or fields.$limit
: Limits the number of documents passed to the next stage.$lookup
: Performs a left outer join to another collection.
Example: Aggregation Pipeline
Here is an example of an aggregation pipeline that performs several operations on a collection of sales data:
Example: Aggregation Pipeline
pipeline = [ {"$match": {"status": "A"}}, {"$group": { "_id": "$cust_id", "total": {"$sum": "$amount"} }}, {"$sort": {"total": -1}}, {"$limit": 5} ] result = db.sales.aggregate(pipeline) for doc in result: print(doc)
Advanced Usage
Advanced usage of the aggregation pipeline includes using expressions, conditional logic, and custom functions. Here are some examples:
Using Expressions
Expressions allow you to compute values in the aggregation pipeline:
Example: Using Expressions
pipeline = [ {"$project": { "item": 1, "discountedPrice": {"$multiply": ["$price", 0.9]} }} ] result = db.products.aggregate(pipeline) for doc in result: print(doc)
Conditional Logic
Use conditional logic to apply different operations based on conditions:
Example: Conditional Logic
pipeline = [ {"$project": { "item": 1, "discount": { "$cond": { "if": {"$gte": ["$price", 100]}, "then": 20, "else": 10 } } }} ] result = db.products.aggregate(pipeline) for doc in result: print(doc)
Custom Functions
You can use custom JavaScript functions in the $function
stage:
Example: Custom Functions
pipeline = [ {"$project": { "item": 1, "customField": { "$function": { "body": "function(price) { return price * 1.1; }", "args": ["$price"], "lang": "js" } } }} ] result = db.products.aggregate(pipeline) for doc in result: print(doc)
Conclusion
In this tutorial, you have learned about advanced usage of the aggregation pipeline in MongoDB. The aggregation pipeline provides a flexible and powerful framework for data analysis and transformation, making it an essential tool for working with MongoDB data.