Aggregation framework in MongoDB

  • The aggregation framework in MongoDB is a powerful tool for processing data and transforming it into a desired result. It allows you to perform complex data manipulations such as filtering, grouping, sorting, reshaping, and calculating aggregate values directly within the database.
  • The aggregation framework works by creating a pipeline of operations that documents pass through. Each stage in the pipeline transforms the documents in some way, either by filtering them, modifying them, or aggregating values. The documents then proceed to the next stage, where further transformations can be applied.
Key Concepts of MongoDB Aggregation Framework
  • Aggregation Pipeline:
    • A sequence of stages that are processed in order.
    • Each stage transforms the documents that pass through it.
    • The output of one stage is the input to the next.
  • Stages:
    • Each stage in the pipeline performs a specific operation on the data (e.g., filtering, grouping, sorting).
    • Some common stages include $match, $group, $project, $sort, and $limit.
  • Operators:
    • Operators define the transformations or operations applied in each stage.
    • Examples include $sum, $avg, $min, $max, $addToSet, $push, etc.
Aggregation Pipeline Stages
  • $match:
    • Filters documents based on the given criteria, similar to a find() query.
    • It is usually the first stage to limit the number of documents that need further processing.
  • Example:

    db.orders.aggregate([
        { $match: { status: "shipped" } }
    ])

  • This query filters documents in the orders collection where status is "shipped".
  • $group:
    • Groups documents by a specified field and applies aggregate functions (e.g., sum, average) to each group.
    • It is similar to the GROUP BY clause in SQL.
  • Example:

    db.orders.aggregate([
        { $group: { _id: "$customerId", totalAmount: { $sum: "$amount" } } }
    ])

  • This groups the documents by customerId and calculates the total amount for each customer by summing the amount field.
  • $project:
    • Reshapes documents by including or excluding fields.
    • You can also create new computed fields in this stage.
  • Example:

    db.orders.aggregate([
        {
            $project: {
                customerId: 1,
                orderDate: 1,
                totalAmount: {
                    $multiply: ["$quantity", "$price"]
                }
            }
        }
    ])

  • This projects the customerId, orderDate, and creates a new totalAmount field by multiplying quantity and price.
  • $sort:
    • Sorts the documents in the pipeline by the specified field in either ascending (1) or descending (-1) order.
  • Example:

    db.orders.aggregate([
        { $sort: { orderDate: -1 } }
    ])

  • This sorts the orders by orderDate in descending order.
  • $limit:
    • Limits the number of documents passing through the pipeline.
  • Example:

    db.orders.aggregate([
        { $limit: 5 }
    ])

  • This limits the results to the first 5 documents.
  • $skip:
    • Skips a specified number of documents before passing the remaining documents to the next stage.
  • Example:

    db.orders.aggregate([
        { $skip: 10 }
    ])

  • This skips the first 10 documents.
  • $unwind:
    • Deconstructs an array field in each document into multiple documents, each containing a single element of the array.
  • Example:

    db.orders.aggregate([
        { $unwind: "$items" }
    ])

  • If the items field is an array, this operation will create a separate document for each item in the array.
  • $lookup:
    • Performs a left outer join with another collection.
  • Example:

    db.orders.aggregate([
        {
            $lookup: {
                from: "customers",
                localField: "customerId",
                foreignField: "_id",
                as: "customerDetails"
            }
        }
    ])

  • This joins the orders collection with the customers collection, matching the customerId field with the _id field in the customers collection.
  • $addFields:
    • Adds new fields to documents or modifies existing fields.
  • Example:

    db.orders.aggregate([
        { $addFields: { totalCost: { $multiply: ["$price", "$quantity"] } } }
    ])

  • This creates a new field totalCost by multiplying the price and quantity fields.
  • $out:
    • Writes the resulting documents to a new collection.
  • Example:

    db.orders.aggregate([
        { $match: { status: "shipped" } },
        { $out: "shippedOrders" }
    ])

  • This writes all documents with a status of "shipped" to a new collection called shippedOrders.
Aggregation Operators
  • Mathematical Operators:
    • $sum: Adds values together.
    • $avg: Calculates the average.
    • $min/$max: Returns the minimum or maximum value.
  • Example:

    db.orders.aggregate([
        { $group: { _id: null, totalAmount: { $sum: "$amount" }, avgAmount: { $avg: "$amount" } } }
    ])

  • Array Operators:
    • $push: Adds elements to an array.
    • $addToSet: Adds unique elements to an array.
  • Example:

    db.orders.aggregate([
        { $group: { _id: "$customerId", orders: { $push: "$_id" } } }
    ])

  • String Operators:
    • $concat: Concatenates strings.
    • $substr: Extracts substrings.
  • Example:

    db.users.aggregate([
        { $project: { fullName: { $concat: ["$firstName", " ", "$lastName"] } } }
    ])

  • Conditional Operators:
    • $cond: Conditional logic (like an if-else).
    • $ifNull: Returns a default value if the field is null.
  • Example:

    db.orders.aggregate([
        {
            $project: {
                status: {
                    $cond: {
                        if: {
                            $gt: ["$amount", 100]
                        },
                        then: "High",
                        else: "Low"
                    }
                }
            }
        }
    ])


Example Aggregation Pipeline
  • Let's say you want to find the total sales per customer for orders that were placed in 2023, and you want the results sorted by total sales.

    db.orders.aggregate([
        {
            $match: {
                orderDate: {
                    $gte: ISODate("2023-01-01"),
                    $lte: ISODate("2023-12-31")
                }
            }
        },
        {
            $group: {
                _id: "$customerId",
                totalSales: {
                    $sum: "$amount"
                }
            }
        },
        {
            $sort: {
                totalSales: -1
            }
        }
    ])

  • $match: Filters the documents to only include orders placed in 2023.
  • $group: Groups the documents by customerId and calculates the total sales for each customer.
  • $sort: Sorts the results by totalSales in descending order.
Benefits of Aggregation Framework
  • Efficiency: Aggregation operations are performed on the database server, reducing the amount of data that needs to be transferred to the client.
  • Flexibility: The aggregation framework provides a wide variety of stages and operators, allowing for complex data transformations and calculations.
  • Powerful Data Processing: It can perform tasks like grouping, filtering, joining, and reshaping data, eliminating the need to perform these operations in the application code.
Limitations of Aggregation Framework
  • Complexity: For very large datasets or highly complex queries, aggregation pipelines can become complex to design and debug.
  • Memory Usage: Aggregation operations that require large amounts of data to be held in memory can be resource-intensive.
  • Sharding: Aggregation operations can be more challenging to optimize when working with sharded collections.
Conclusion
  • The MongoDB Aggregation Framework is a robust and flexible tool for data analysis and transformation. It allows you to create powerful pipelines that can process and manipulate data in various ways, all within the database. By using different stages like $match, $group, $project, and operators, you can build complex queries that provide meaningful insights from your data.

No comments:

Post a Comment

Mathematical Aggregation Functions ($mod, $abs, $round, $exp, $log, $log10, $pow, $sqrt, $trunc, $sin, $cos, $tan, $degreesToRadians, $radiansToDegrees, $round, $ceil, $floor)

MongoDB  Mathematical  Aggregation Functions with Examples Step 1:  Insert a Dataset into MongoDB Collection First, we'll insert a sampl...