Professional Documents
Culture Documents
• 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
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
• 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: 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)
• 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’})