MongoDB aggregation operations are used to process the data and return the processed records. The aggregation operations group the multiple Documents and perform various operations on that group of Documents to return a single value.
aggregate() Method
MongoDB uses the aggregate() method to perform the aggregation. Let us see the aggregate() method in detail with the below example.
Syntax:
The following is the syntax of aggregate() method.
Example:
To perform the aggregation operation, we have created a new Collection with the name "orders_detail" that is present under the "mongodb_test" database and contains the following Documents. Please follow the below steps to create the Collection "orders_detail".
{ _id: 101, customer_id: "Agard Eric", order_date: new Date("2019-03-01"), item_price: 25, items_detail: [ { sku: "oranges", quantity: 5, item_price: 2.5 }, { sku: "apples", quantity: 5, item_price: 2.5 } ], status: "A" },
{ _id: 102, customer_id: "Agard Eric", order_date: new Date("2019-03-08"), item_price: 70, items_detail: [ { sku: "oranges", quantity: 8, item_price: 2.5 }, { sku: "chocolates", quantity: 5, item_price: 10 } ], status: "A" },
{ _id: 103, customer_id: "Brown Jr Ronald ", order_date: new Date("2019-03-08"), item_price: 50, items_detail: [ { sku: "oranges", quantity: 10, item_price: 2.5 }, { sku: "pears", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 104, customer_id: "Brown Jr Ronald ", order_date: new Date("2019-03-18"), item_price: 25, items_detail: [ { sku: "oranges", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 105, customer_id: "Brown Jr Ronald ", order_date: new Date("2018-03-19"), item_price: 50, items_detail: [ { sku: "chocolates", quantity: 5, item_price: 10 } ], status: "A"},
{ _id: 106, customer_id: "Chester Shaunta", order_date: new Date("2018-03-19"), item_price: 35, items_detail: [ { sku: "carrots", quantity: 10, item_price: 1.0 }, { sku: "apples", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 107, customer_id: "Chester Shaunta", order_date: new Date("2018-03-20"), item_price: 25, items_detail: [ { sku: "oranges", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 108, customer_id: "Dixon Curtis", order_date: new Date("2017-03-20"), item_price: 75, items_detail: [ { sku: "chocolates", quantity: 5, item_price: 10 }, { sku: "apples", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 109, customer_id: "Dixon Curtis", order_date: new Date("2017-03-20"), item_price: 55, items_detail: [ { sku: "carrots", quantity: 5, item_price: 1.0 }, { sku: "apples", quantity: 10, item_price: 2.5 }, { sku: "oranges", quantity: 10, item_price: 2.5 } ], status: "A" },
{ _id: 110, customer_id: "Dixon Curtis", order_date: new Date("2017-03-23"), item_price: 25, items_detail: [ { sku: "oranges", quantity: 10, item_price: 2.5 } ], status: "A" }])
Output:
We can see from the below output that the Collection with the name "orders_detail" has been created.
Now let us see some examples of the MongoDB aggregate Method with aggregation expressions.
$sum expression with aggregate() Method
In the below example, in the first stage, we will use the $match expression to filter the Document of "orders_detail" Collection who has the status= "A", in the second stage we will use $group expression to group the Document by customer_id and calculate the sum of each customer_id.
Example:
{ $match: { status: "A" } }, { $group: { _id: "$customer_id", total: { $sum: "$item_price" } } }
])
Output:
$avg expression with aggregate() Method
The below example will fetch the average value of all matching Documents where status= "A".
Example:
{ $match: { status: "A" } }, { $group: { _id: "$customer_id", total: { $avg: "$item_price" } } }
])
Output:
$min expression with aggregate() Method
The below example will fetch the minimum value of all matching Documents where status= "A".
Example:
{ $match: { status: "A" } }, { $group: { _id: "$customer_id", total: { $min: "$item_price" } } }
])
Output:
$max expression with aggregate() Method
The below example will fetch the maximum value of all matching Documents where status= "A".
Example:
{ $match: { status: "A" } }, { $group: { _id: "$customer_id", total: { $max: "$item_price" } } }
])
Output:
$first expression with aggregate() Method
The $first expression returns the first Document from the group of Documents.
Example:
{ $group: { _id: "$customer_id", total: { $first: "$item_price" } } }
])
Output:
$last expression with aggregate() Method
The $last expression returns the last Document from the group of Documents.
Example:
{ $group: { _id: "$customer_id", total: { $last: "$item_price" } } }
])