You are on page 1of 18

AGGREGATION

NOSQL DATA SOLUTIONS


WHAT IS AGGREGATION

• The operation that is used for processing various types of data in the
collection, which returns a calculated result.
• The concept of aggregation mainly clusters out your data from multiple
different documents which are then used and operates in lots of ways (on
these clustered data) to return a combined result which can bring new
information to the existing database.
3 WAYS OF AGGREGATION

• The aggregation pipeline.


• The map-reduce function. (Not in this module)
• Single purpose aggregation methods.
THE METHOD IN MONGODB

• cluster out the records in the form of a collection


• employed for providing operations (total number(sum), mean, minimum and
maximum, etc.)
• Syntax
db.collection_name.aggregate(pipeline, [options])
PIPELINE OPERATION

Stages Description
$project Passes along the documents with the requested fields to the next stage in
the pipeline
$lookup Performs a left outer join to an unsharded collection in the same database
to filter in documents from the “joined” collection for processing.
$match Filters the document stream to allow only matching documents to pass
unmodified into the next pipeline stage.
$unwind Deconstructs an array field from the input documents to output a
document for each element.
$count Passes a document to the next stage that contains a count of the number
of documents input to the stage.
MULTI-STAGE PIPELINE

• Documents enter a multi-stage pipeline that transforms the documents into


aggregated results.
• The MongoDB aggregation pipeline consists of stages. Each stage transforms the
documents as they pass through the pipeline.
• Pipeline stages do not need to produce one output document for every input
document; e.g., some stages may generate new documents or filter out documents.
• Note: you cannot process some of the multi-stage pipeline in MongoDB Compress
for Free Tier Cluster
Relationshi
p?
$project

• A function similar to find()


• Syntax:
{ $project: { "<field1>": 0, "<field2>": 0, ... } }
• Example: Display customer name and customer address
db.customer.aggregate([{$project:{
_id:0,
customer_name: 1,
customer_address: 1}}])
• Note: if the key is embedded in embedded document, remind to use the quotation
mark. (such as: ‘customer.name’)
$match

• Filters the documents to pass only the documents that match the specified
condition(s) to the next pipeline stage.
• Syntax:
{ $match: { $expr: { <aggregation expression> } } }

• Example: Display customer with city in Hong Kong


db.customer.aggregate([{$match: {city: "Hong Kong"}}])
$match + $project *

• Example: List the customer name and address with city in Hong Kong
db.customer.aggregate([
{$match: {city: "Hong Kong"}},
{project: {_id: 0, customer_name:1, customer_address:1}}
])
$count

• Passes a document to the next stage that contains a count of the number of
documents input to the stage.
• Example: Count the number of customer with city in Hong Kong
db.customer.aggregate([
{$match: {city: "Hong Kong"}},
{$count: "city"}])
$group

• Groups input documents by the specified _id expression and for each
distinct grouping, outputs a document. The _id field of each output
document contains the unique group by value.
• Example: Count the number of customers for each city
db.customer.aggregate([{
$group: {_id: "$city", total: {$sum: 1}}
}])
CALCULATION WITH $sum

• Example: List the in-stock value (unit_price * on_hand) for each product
db.product.aggregate([{
$group: {
_id: "$product_name",
totalValue: {
$sum: {$multiply: ["$unit_price", "$on_hand"]}
}
}
}])
AGGREGATION PIPELINE EXPRESSION
OPERATORS
• Operator expressions are similar to functions that take arguments. In
general, these expressions take an array of arguments and have the following
form:
{ <operator>: [ <argument1>, <argument2> ... ] }

• More Detail:
https://docs.mongodb.com/manual/reference/operator/aggregation/
$lookup

• Performs a left outer join to an unsharded collection in the same database to filter
in documents from the “joined” collection for processing.
• Syntax:
{ $lookup: {
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from" collection>,
as: <output array field>
} }
$lookup (example)

• Example: make the result of orders with customer details


db.orders.aggregate({
$lookup: {
from:'customer’,
localField:'customer_id’,
foreignField:'_id’,
as:'customer’}})

• Any problem exist?


$unwind

• Deconstructs an array field from the input documents to output a document


for each element.

• Syntax
{ $unwind: <field path> }
$lookup and $unwind

• Example: make the result of orders with customer details (No array
version)
db.orders.aggregate(
{$lookup: {
from:'customer’,
localField:'customer_id’,
foreignField:'_id’,
as:'customer'}},
{$unwind: '$customer’})

You might also like