1
Mongodb Aggregation
Amit Ghosh
Software Engineer
Jeeon Bangladesh Ltd.
2
• MongoDB Aggregation
• Some Aggregation Pipeline Stages
• Some Aggregation Pipeline Operator
Objective
3
What is MongoDB aggregation ?
Processing data records
and return computed
results.
4
Aggregation Pipeline Stages
$match
Filters the documents to pass only the documents that
match the specified condition(s) to the next pipeline stage.
5
Example
{ $match : { author : "dave" } }
{ "_id":ObjectId("512bc95fe835e68f199c8686"),
"author":"dave",
"score":80,
"views":100},
{"_id":ObjectId("512bc962e835e68f199c8687"),
"author":"dave",
"score":85,
"views":521},
{ "_id":ObjectId("55f5a192d4bede9ac365b257"),
"author":"ahn",
"score":60,
"views":1000}
{ "_id":ObjectId("55f5a192d4bede9ac365b258"),
"author":"li",
"score":55,
"views":5000}
6
$lookup
Performs a left outer join
Reshape documents
 Passes these reshaped
documents to the next stage.
{
$lookup:
{
from: <collection to join>,
localField: <field from the input documents>,
foreignField: <field from the documents of the "from"
collection>,
as: <output array field>
}
}
7
Example
{ "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
{ "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
{ "_id" : 3 }
{ "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 },
{ "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 },
{ "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 },
{ "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 },
{ "_id" : 5, "sku": null, description: "Incomplete" },
{ "_id" : 6 }
8
inventory
orders
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
}
Example
9
{
"_id" : 1,
"item" : "almonds",
"price" : 12,
"quantity" : 2,
"inventory_docs" : [
{ "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 }
]
}
{
"_id" : 2,
"item" : "pecans",
"price" : 20,
"quantity" : 1,
"inventory_docs" : [
{ "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 }
]
}
{
"_id" : 3,
"inventory_docs" : [
{ "_id" : 5, "sku" : null, "description" : "Incomplete" },
{ "_id" : 6 }
]
}
SELECT *, inventory_docs
FROM orders
WHERE inventory_docs IN (SELECT *
FROM inventory
WHERE sku= orders.item);
$project
Passes along the documents with the requested
fields to the next stage in the pipeline.
The specified fields can be existing fields from the
input documents or newly computed fields.
10
{ $project: { <specification(s)> } }
Example
11
{
"_id" : 1,
"title" : "abc123",
"author" : { "last" : "zzz", "first" : "aaa" }
}
{
"_id" : 1,
title: "abc123",
isbn: "0001122223334",
author: { last: "zzz", first: "aaa" },
copies: 5
}
{ $project : { title : 1 , author : 1 } }
Why _id ?
$unwind
Deconstructs an array field from the input
documents to output a document for each element.
Each output document is the input document with
the value of the array field replaced by the element.
12
{ $unwind: <field path> }
{
$unwind:
{
path: <field path>,
includeArrayIndex: <string>,
preserveNullAndEmptyArrays: <boolean>
}
}
Example
{
"_id" : 1, "item" : "ABC1", sizes: [ "S", "M", "L"]
}
13
{ $unwind : "$sizes" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "S" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "M" }
{ "_id" : 1, "item" : "ABC1", "sizes" : "L" }
Other Aggregation Pipeline Stages
14
• $addFields
• $bucket
• $bucketAuto
• $collStats
• $count
• $facet
• $geoNear
• $graphLookup
• $group
• $indexStats
• $limit
• $listSessions
• $lookup
• $match
• $out
• $project
• $redact
• $replaceRoot
• $sample
• $skip
• $sort
• $sortByCount
Aggregation Pipeline Operators
15
Arithmetic Expression
Operators
$abs
$add
$ceil
$divide
$exp
$floor
$log
$log10
$mod
$multiply
$pow
$sqrt
$subtract
$ln
Boolean Operator
$and
$not
$or
Comparison
Expression
Operators
$cmp
$eq
$gt
$gte
$lt
$lte
$ne
16
Aggregation Pipeline
17
Any Question ?
18
Thanks

MongoDB Aggregation

  • 1.
    1 Mongodb Aggregation Amit Ghosh SoftwareEngineer Jeeon Bangladesh Ltd.
  • 2.
    2 • MongoDB Aggregation •Some Aggregation Pipeline Stages • Some Aggregation Pipeline Operator Objective
  • 3.
    3 What is MongoDBaggregation ? Processing data records and return computed results.
  • 4.
  • 5.
    $match Filters the documentsto pass only the documents that match the specified condition(s) to the next pipeline stage. 5
  • 6.
    Example { $match :{ author : "dave" } } { "_id":ObjectId("512bc95fe835e68f199c8686"), "author":"dave", "score":80, "views":100}, {"_id":ObjectId("512bc962e835e68f199c8687"), "author":"dave", "score":85, "views":521}, { "_id":ObjectId("55f5a192d4bede9ac365b257"), "author":"ahn", "score":60, "views":1000} { "_id":ObjectId("55f5a192d4bede9ac365b258"), "author":"li", "score":55, "views":5000} 6
  • 7.
    $lookup Performs a leftouter join Reshape documents  Passes these reshaped documents to the next stage. { $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } } 7
  • 8.
    Example { "_id" :1, "item" : "almonds", "price" : 12, "quantity" : 2 }, { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 }, { "_id" : 3 } { "_id" : 1, "sku" : "almonds", description: "product 1", "instock" : 120 }, { "_id" : 2, "sku" : "bread", description: "product 2", "instock" : 80 }, { "_id" : 3, "sku" : "cashews", description: "product 3", "instock" : 60 }, { "_id" : 4, "sku" : "pecans", description: "product 4", "instock" : 70 }, { "_id" : 5, "sku": null, description: "Incomplete" }, { "_id" : 6 } 8 inventory orders { $lookup: { from: "inventory", localField: "item", foreignField: "sku", as: "inventory_docs" } }
  • 9.
    Example 9 { "_id" : 1, "item": "almonds", "price" : 12, "quantity" : 2, "inventory_docs" : [ { "_id" : 1, "sku" : "almonds", "description" : "product 1", "instock" : 120 } ] } { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1, "inventory_docs" : [ { "_id" : 4, "sku" : "pecans", "description" : "product 4", "instock" : 70 } ] } { "_id" : 3, "inventory_docs" : [ { "_id" : 5, "sku" : null, "description" : "Incomplete" }, { "_id" : 6 } ] } SELECT *, inventory_docs FROM orders WHERE inventory_docs IN (SELECT * FROM inventory WHERE sku= orders.item);
  • 10.
    $project Passes along thedocuments with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields. 10 { $project: { <specification(s)> } }
  • 11.
    Example 11 { "_id" : 1, "title": "abc123", "author" : { "last" : "zzz", "first" : "aaa" } } { "_id" : 1, title: "abc123", isbn: "0001122223334", author: { last: "zzz", first: "aaa" }, copies: 5 } { $project : { title : 1 , author : 1 } } Why _id ?
  • 12.
    $unwind Deconstructs an arrayfield from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element. 12 { $unwind: <field path> } { $unwind: { path: <field path>, includeArrayIndex: <string>, preserveNullAndEmptyArrays: <boolean> } }
  • 13.
    Example { "_id" : 1,"item" : "ABC1", sizes: [ "S", "M", "L"] } 13 { $unwind : "$sizes" } { "_id" : 1, "item" : "ABC1", "sizes" : "S" } { "_id" : 1, "item" : "ABC1", "sizes" : "M" } { "_id" : 1, "item" : "ABC1", "sizes" : "L" }
  • 14.
    Other Aggregation PipelineStages 14 • $addFields • $bucket • $bucketAuto • $collStats • $count • $facet • $geoNear • $graphLookup • $group • $indexStats • $limit • $listSessions • $lookup • $match • $out • $project • $redact • $replaceRoot • $sample • $skip • $sort • $sortByCount
  • 15.
    Aggregation Pipeline Operators 15 ArithmeticExpression Operators $abs $add $ceil $divide $exp $floor $log $log10 $mod $multiply $pow $sqrt $subtract $ln Boolean Operator $and $not $or Comparison Expression Operators $cmp $eq $gt $gte $lt $lte $ne
  • 16.
  • 17.
  • 18.