SlideShare a Scribd company logo
2
Most read
3
Most read
15
Most read
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

More Related Content

What's hot (20)

PDF
MongoDB WiredTiger Internals
Norberto Leite
 
PPTX
MongoDB
nikhil2807
 
PDF
An introduction to MongoDB
César Trigo
 
PPTX
Introduction to MongoDB
NodeXperts
 
PPTX
Introduction to MongoDB.pptx
Surya937648
 
PPTX
Mongo DB Presentation
Jaya Naresh Kovela
 
ZIP
NoSQL databases
Harri Kauhanen
 
PPTX
NOSQL Databases types and Uses
Suvradeep Rudra
 
PDF
MongodB Internals
Norberto Leite
 
PPTX
Nodejs functions & modules
monikadeshmane
 
PDF
GraphQL ♥︎ GraphDB
GraphRM
 
PDF
Introduction to MongoDB
Mike Dirolf
 
PDF
Using MongoDB as a high performance graph database
Chris Clarke
 
PPTX
Mongo db intro.pptx
JWORKS powered by Ordina
 
PPTX
Sharding Methods for MongoDB
MongoDB
 
ODP
Introduction to MongoDB
Dineesha Suraweera
 
PDF
MongoDB Sharding Fundamentals
Antonios Giannopoulos
 
PPTX
Postgresql
NexThoughts Technologies
 
PPTX
Introduction to Sharding
MongoDB
 
MongoDB WiredTiger Internals
Norberto Leite
 
MongoDB
nikhil2807
 
An introduction to MongoDB
César Trigo
 
Introduction to MongoDB
NodeXperts
 
Introduction to MongoDB.pptx
Surya937648
 
Mongo DB Presentation
Jaya Naresh Kovela
 
NoSQL databases
Harri Kauhanen
 
NOSQL Databases types and Uses
Suvradeep Rudra
 
MongodB Internals
Norberto Leite
 
Nodejs functions & modules
monikadeshmane
 
GraphQL ♥︎ GraphDB
GraphRM
 
Introduction to MongoDB
Mike Dirolf
 
Using MongoDB as a high performance graph database
Chris Clarke
 
Mongo db intro.pptx
JWORKS powered by Ordina
 
Sharding Methods for MongoDB
MongoDB
 
Introduction to MongoDB
Dineesha Suraweera
 
MongoDB Sharding Fundamentals
Antonios Giannopoulos
 
Introduction to Sharding
MongoDB
 

Similar to MongoDB Aggregation (20)

PDF
Aggregation Framework MongoDB Days Munich
Norberto Leite
 
PPTX
The Aggregation Framework
MongoDB
 
PPTX
MongoDB's New Aggregation framework
Chris Westin
 
PPTX
Aggregation Presentation for databses (1).pptx
plvdravikumarit
 
PPTX
mongodb-aggregation-may-2012
Chris Westin
 
PPTX
Aggregation in MongoDB
Kishor Parkhe
 
PPTX
The Aggregation Framework
MongoDB
 
PPTX
Query for json databases
Binh Le
 
PPTX
MongoDB Aggregation MongoSF May 2011
Chris Westin
 
KEY
MongoDB Aggregation Framework
Tyler Brock
 
PPT
Schema Design by Chad Tindel, Solution Architect, 10gen
MongoDB
 
PDF
MongoDB FabLab León
Juan Antonio Roy Couto
 
PDF
Using MongoDB and Python
Mike Bright
 
PDF
2016 feb-23 pyugre-py_mongo
Michael Bright
 
PPTX
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
MongoDB
 
PPTX
Joins and Other MongoDB 3.2 Aggregation Enhancements
Andrew Morgan
 
PPTX
1403 app dev series - session 5 - analytics
MongoDB
 
PDF
MongoDB Meetup
Maxime Beugnet
 
ODP
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
PDF
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
Aggregation Framework MongoDB Days Munich
Norberto Leite
 
The Aggregation Framework
MongoDB
 
MongoDB's New Aggregation framework
Chris Westin
 
Aggregation Presentation for databses (1).pptx
plvdravikumarit
 
mongodb-aggregation-may-2012
Chris Westin
 
Aggregation in MongoDB
Kishor Parkhe
 
The Aggregation Framework
MongoDB
 
Query for json databases
Binh Le
 
MongoDB Aggregation MongoSF May 2011
Chris Westin
 
MongoDB Aggregation Framework
Tyler Brock
 
Schema Design by Chad Tindel, Solution Architect, 10gen
MongoDB
 
MongoDB FabLab León
Juan Antonio Roy Couto
 
Using MongoDB and Python
Mike Bright
 
2016 feb-23 pyugre-py_mongo
Michael Bright
 
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
MongoDB
 
Joins and Other MongoDB 3.2 Aggregation Enhancements
Andrew Morgan
 
1403 app dev series - session 5 - analytics
MongoDB
 
MongoDB Meetup
Maxime Beugnet
 
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
Ad

More from Amit Ghosh (13)

PPTX
Custom object detection using Deep Learning
Amit Ghosh
 
PDF
Data Set Analysis
Amit Ghosh
 
PPTX
Cloud platform comparison
Amit Ghosh
 
PPTX
Gitlab runner in aws
Amit Ghosh
 
PPTX
Restaurant ordering system
Amit Ghosh
 
PPTX
System Analysis And Development Course
Amit Ghosh
 
PPTX
Bumble bee
Amit Ghosh
 
PPTX
Convex hull presentation
Amit Ghosh
 
PPTX
Smart City Hackathon Presentation
Amit Ghosh
 
PPTX
Pattern presentation
Amit Ghosh
 
PPTX
Shell & Shell Script
Amit Ghosh
 
PPTX
Shell & Shell Script
Amit Ghosh
 
PPTX
System call (Fork +Exec)
Amit Ghosh
 
Custom object detection using Deep Learning
Amit Ghosh
 
Data Set Analysis
Amit Ghosh
 
Cloud platform comparison
Amit Ghosh
 
Gitlab runner in aws
Amit Ghosh
 
Restaurant ordering system
Amit Ghosh
 
System Analysis And Development Course
Amit Ghosh
 
Bumble bee
Amit Ghosh
 
Convex hull presentation
Amit Ghosh
 
Smart City Hackathon Presentation
Amit Ghosh
 
Pattern presentation
Amit Ghosh
 
Shell & Shell Script
Amit Ghosh
 
Shell & Shell Script
Amit Ghosh
 
System call (Fork +Exec)
Amit Ghosh
 
Ad

Recently uploaded (20)

PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 

MongoDB Aggregation

  • 1. 1 Mongodb Aggregation Amit Ghosh Software Engineer Jeeon Bangladesh Ltd.
  • 2. 2 • MongoDB Aggregation • Some Aggregation Pipeline Stages • Some Aggregation Pipeline Operator Objective
  • 3. 3 What is MongoDB aggregation ? Processing data records and return computed results.
  • 5. $match Filters the documents to 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 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
  • 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 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)> } }
  • 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 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> } }
  • 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 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
  • 15. 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