SlideShare a Scribd company logo
MongoDB Aggregations
Mikhail Asavkin
@limenutt
Soup du jour
● My company
● Business context
● Aggregation basics
● Coding!
● Take home task
Workshop  mongo aggregations [musescodejs 16.11.19]
We all interact with property every
day.
We build, sell, buy, rent, lease,
own, use and admire it.
At Skychute
We improve experiences people
have with property.
We use technology to provide
tenants, owners, investors, agents,
developers and builders with
greater experience and outcomes.
At the heart of our company,
our greatest asset are
brave engineers
passionate about property.
Join
our cause
Meet the data!
@limenutt
@limenutt
MS L
M LS
M LS
M LS
The Product
M LS
The Product
$29
$29
$29
The Variant
Product: T-Shirt #42
Colour: Yolk
Size: S
S @limenutt
S
The Variant
Product: T-Shirt #42
Colour: Snow
Size: S
@limenutt
M
The Variant
Product: T-Shirt #42
Colour: Snow
Size: M
@limenutt
M
The Variant
Product: T-Shirt #42
Colour: Yolk
Size: M
@limenutt
@limenutt
Categories
S/S Tee
(T-Shirt)
@limenutt
Categories
S/S Tee
(T-Shirt)
S/S Polo
@limenutt
Categories
S/S Tee
(T-Shirt)
S/S Polo S/S Shirt
@limenutt
Categories
S/S Tee
(T-Shirt)
S/S Polo S/S Shirt L/S Shirt
@limenutt
@limenutt
Crew neck V-neck
@limenutt
Tags
Crew neck V-neck
@limenutt
Tags
Crew neck Slim V-neck Regular
@limenutt
Tags
Crew neck Slim
Plain
V-neck Regular
Plain @limenutt
Tags
Crew neck Slim
CasualPlain
V-neck Regular
Smart-CasualPlain @limenutt
Tag buckets
Crew neck Slim
CasualPlain
V-neck Regular
Smart-CasualPlain @limenutt
Tag buckets
Crew neck Slim
CasualPlain
V-neck Regular
Smart-CasualPlain @limenutt
Tag buckets
Crew neck Slim
CasualPlain
V-neck Regular
Smart-CasualPlain @limenutt
Tag buckets
Crew neck Slim
CasualPlain
V-neck Regular
Smart-CasualPlain @limenutt
Order
Order
customer
owner
salesperson
variant 1
variant 2
variant 3 @limenutt
Product
Category bucket
variants
Variant
category
brand
tags
Brand
Category
categories
Tag bucket
tagBuckets
Tag
tags
Order
variants
customer
owner User
Customer
Colour
product
supplierColours
equivalent
Schema
@limenutt
Questions
about the data?
@limenutt
Querying data
from MongoDB
@limenutt
MongoDB Basics
● MongoDB = Document-oriented database
● Document = record
● Document structure is almost JSON
● Documents belong to a Collection
● Database has many Collections
@limenutt
MongoDB Basics: Sample document
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB Basics: Simple query
@limenutt
MongoDB
Aggregation Pipeline
@limenutt
● FAST: 10x faster than doing the same in the backend
● EASY TO USE: Build a complex report in a few minutes
Why use Aggregation Pipeline
@limenutt
● Aggregation is a list of operations
What is Aggregation Pipeline
@limenutt
● Aggregation is a list of operations
● Operations performed in order
What is Aggregation Pipeline
@limenutt
● Aggregation is a list of operations
● Operations performed in order
● Output of the previous operation is the input of the next
one, hence Pipeline
What is Aggregation Pipeline
@limenutt
● Aggregation is a list of operations
● Operations performed in order
● Output of the previous operation is the input of the next
one, hence Pipeline
● It’s mapreduce on steroids
What is Aggregation Pipeline
@limenutt
$match( or )
$group(‘$colour’)
collection(‘shapes’)
@limenutt
$match( or )
$group(‘$colour’)
collection(‘shapes’)
@limenutt
$match( or )
$group(‘$colour’)
collection(‘shapes’)
@limenutt
$match( or )
$group(‘$colour’)
collection(‘shapes’)
Workshop  mongo aggregations [musescodejs 16.11.19]
Aggregation Pipeline
basics
@limenutt
Aggregation pipeline:
@limenutt
Aggregation pipeline:
Aggregation stage 1
Aggregation stage 2
@limenutt
@limenutt
Aggregation stage name
@limenutt
Aggregation stage name
Aggregation stage body
@limenutt
Starter app
@limenutt
bit.ly/mongoapp
@limenutt
1. Open the project in your
favourite editor (vscode)
@limenutt
1. Open the project in your
favourite editor (vscode)
2. Open the terminal window
@limenutt
1. Open the project in your
favourite editor (vscode)
2. Open the terminal window
3. Run
npm i && npm run start-w
src/app.ts
@limenutt
src/app.ts
Your password goes there
@limenutt
src/app.ts
Here goes your
aggregation pipeline
@limenutt
src/app.ts
@limenutt
src/app.ts
This is the collection you are
fetching the data from.
You will need to change it
depending on the task @limenutt
Please, keep the docs open:
@limenutt
Please, keep the docs open:
● Aggregation Pipeline
Stages
@limenutt
Please, keep the docs open:
● Aggregation Pipeline
Stages
● Aggregation Pipeline
Operators @limenutt
Let’s code!
@limenutt
T: Filtering ($match stage)
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
Operator ‘equals’ [a, b]
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
Operator ‘equals’ [a, b]
Reference to the field postcode
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
Operator ‘equals’ [a, b]
Reference to the field postcode
@limenutt
T: Filtering ($match stage)
E: List all orders with postcode 2000
Operator ‘equals’ [a, b]
Reference to the field postcode
@limenutt
T: Filtering ($match stage)
1. List all orders in NSW
2. List all products under $50 (retailPrice)
3. List all products within $50 — $100
@limenutt
T: Sorting ($sort stage)
E: Orders sorted by creation date
@limenutt
T: Sorting ($sort stage)
E: Orders sorted by creation date
@limenutt
T: Sorting ($sort stage)
1. Products sorted by retail price
ascending
2. Products sorted by retail price
descending
@limenutt
T: Limiting ($limit stage)
E: Top 5 most recent orders
@limenutt
T: Limiting ($limit stage)
E: Top 5 most recent orders
@limenutt
T: Limiting ($limit stage)
E: Top 5 most recent orders
@limenutt
T: Limiting ($limit stage)
1. Top 10 most expensive products
@limenutt
T: Grouping ($group stage)
E: List all order types
@limenutt
T: Grouping ($group stage)
E: List all order types
@limenutt
T: Grouping ($group stage)
1. List all order postcodes
@limenutt
T: Group accumulator operators
E: Average product retail price
@limenutt
T: Group accumulator operators
E: Average product retail price
@limenutt
T: Group accumulator operators
E: Average product retail price
Accumulator operator ‘average’
@limenutt
T: Group accumulator operators
1. Maximum product price
2. Sum of prices of all products
3. Total number of products
@limenutt
T: Filtering + counting
1. Number of orders with type of
Style Lab
2. Number of products over $100
@limenutt
T: Grouping + counting
1. Number of orders by type
2. Number of orders by state
3. Number of colours by name
@limenutt
T: Adding fields and Expressions
E: Products with margin amounts
@limenutt
T: Adding fields and Expressions
E: Products with margin amounts
@limenutt
T: Adding fields and Expressions
E: Products with margin amounts
This stage adds new fields to the documents
@limenutt
T: Adding fields and Expressions
E: Products with margin amounts
This stage adds new fields to the documents
Field expression
@limenutt
T: Adding fields and Expressions
1. Products with margin percentage
2. Top 10 products with the highest
margin percentage
3. Products by number of variants
@limenutt
T: 1-1 Lookups
Product
brand (id)
Brand
name
_id
@limenutt
T: 1-1 Lookups
Product
brand (id)
Brand
name
_id
Product
brand:
name
_id
$lookup
@limenutt
T: 1-1 Lookups ($lookup stage)
E: Products with brand
@limenutt
T: 1-1 Lookups ($lookup stage)
E: Products with brand
@limenutt
T: 1-1 Lookups ($lookup stage)
E: Products with brand
Collection to look up from
Field to match from current collection
Field to match from another collection
Field for the result
@limenutt
T: 1-1 Lookups ($lookup stage)
E: Products with brand
Collection to look up from
Field to match from current collection
Field to match from another collection
Field for the result
The result is always an array
@limenutt
T: 1-1 Lookups ($lookup stage)
E: Products with brand
Collection to look up from
Field to match from current collection
Field to match from another collection
Field for the result
The result is always an array
Turn array into object (for now)@limenutt
T: 1-1 Lookups ($lookup stage)
1. Variants with product
2. Variants with retail price
@limenutt
T: 1-* Lookups
Product
variants [id, id,...id]
Variant
size
_id
@limenutt
T: 1-* Lookups
Product
variants [id, id,...id]
Variant
size
_id
Product
variants:
$lookup
size
_id
size
_id
size
_id
size
_id
@limenutt
T: 1-* Lookups
E: Products with variants
@limenutt
T: 1-* Lookups
E: Products with variants
@limenutt
T: 1-* Lookups
1. Orders with variants and customer
@limenutt
T: Lookups with grouping
1. Number of products by category
2. Average product price by brand
@limenutt
1. Top 10 most expensive Casual products
2. Top 5 cheapest red products
3. Products with tags joined by commas
T: Lookups with expressions
@limenutt
T: Lookups with sub-pipelines
Order
variants [id, id,...id]
Variant
size
_id
customer
@limenutt
T: Lookups with sub-pipelines
Order
variants [id, id,...id]
Variant
size
_id
Variant
orders:
$lookup
customer
_id
customer
_id
customer
_id
customer
_id
customer
@limenutt
T: Lookups with sub-pipelines
@limenutt
T: Lookups with sub-pipelines
@limenutt
T: Lookups with sub-pipelines
Variables to be used in sub-pipeline
@limenutt
T: Lookups with sub-pipelines
Variables to be used in sub-pipeline
This pipeline is executed in orders collection
@limenutt
T: Lookups with sub-pipelines
Variables to be used in sub-pipeline
This pipeline is executed in orders collection
$in [b, a] operator checks if value b is in the array a
Identity comparison used!
@limenutt
T: Lookups with sub-pipelines
1. Products with orders
2. Orders with total amounts
@limenutt
T: Dates
E: Number of orders by year
@limenutt
T: Dates
E: Number of orders by year
@limenutt
T: Dates
1. Sales by year
2. Sales by month-year
3. Sales by a salesperson (owner)
by month-year
@limenutt
T: Advanced
1. Most popular products
2. Brands by margin percentage
3. Top 5 most profitable categories
4. Variants by base colours
5. Most popular base colours by month
6. Number of products per $50 price
bracket, sorted by max price ascending
@limenutt
Thank you!
@limenutt
Keep in touch
@limenutt

More Related Content

PDF
Mongo db aggregation guide
PDF
Mongo db aggregation-guide
PPTX
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
PPTX
Joins and Other Aggregation Enhancements Coming in MongoDB 3.2
PPT
Schema Design by Chad Tindel, Solution Architect, 10gen
PPTX
MongoDB's New Aggregation framework
PPTX
Beyond the Basics 2: Aggregation Framework
PPTX
MongoDB World 2018: Keynote
Mongo db aggregation guide
Mongo db aggregation-guide
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
Joins and Other Aggregation Enhancements Coming in MongoDB 3.2
Schema Design by Chad Tindel, Solution Architect, 10gen
MongoDB's New Aggregation framework
Beyond the Basics 2: Aggregation Framework
MongoDB World 2018: Keynote

Similar to Workshop mongo aggregations [musescodejs 16.11.19] (20)

PPTX
Joins and Other MongoDB 3.2 Aggregation Enhancements
PDF
Simplifying & accelerating application development with MongoDB's intelligent...
PPTX
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
KEY
Optimize drupal using mongo db
PDF
MongoDB .local Paris 2020: Adéo @MongoDB : MongoDB Atlas & Leroy Merlin : et ...
PDF
MongoDB Aggregation Framework
PPTX
Mongo - an intermediate introduction
PDF
MongoDB Meetup
PDF
Streaming SQL with Apache Calcite
PDF
Streaming SQL w/ Apache Calcite
PDF
Webinar: Managing Real Time Risk Analytics with MongoDB
PPTX
1403 app dev series - session 5 - analytics
PDF
Streaming SQL
PPTX
Creating a Single View: Overview and Analysis
PDF
[WSO2Con Asia 2018] Patterns for Building Streaming Apps
PPTX
mongodb-aggregation-may-2012
PPTX
MongoDB.local Sydney 2019: Data Modeling for MongoDB
PPTX
MongoDB Use Cases: Healthcare, CMS, Analytics
PPTX
Low Latency “OLAP” with HBase - HBaseCon 2012
PDF
Value extraction from BBVA credit card transactions. IVAN DE PRADO at Big Dat...
Joins and Other MongoDB 3.2 Aggregation Enhancements
Simplifying & accelerating application development with MongoDB's intelligent...
How to leverage MongoDB for Big Data Analysis and Operations with MongoDB's A...
Optimize drupal using mongo db
MongoDB .local Paris 2020: Adéo @MongoDB : MongoDB Atlas & Leroy Merlin : et ...
MongoDB Aggregation Framework
Mongo - an intermediate introduction
MongoDB Meetup
Streaming SQL with Apache Calcite
Streaming SQL w/ Apache Calcite
Webinar: Managing Real Time Risk Analytics with MongoDB
1403 app dev series - session 5 - analytics
Streaming SQL
Creating a Single View: Overview and Analysis
[WSO2Con Asia 2018] Patterns for Building Streaming Apps
mongodb-aggregation-may-2012
MongoDB.local Sydney 2019: Data Modeling for MongoDB
MongoDB Use Cases: Healthcare, CMS, Analytics
Low Latency “OLAP” with HBase - HBaseCon 2012
Value extraction from BBVA credit card transactions. IVAN DE PRADO at Big Dat...
Ad

More from Mikhail Asavkin (6)

PDF
AI in Robotics: Robocup 2022
PDF
State management with GraphQL [Angular Minsk, Online, 13.06.20]
PDF
What makes a great engineer [Given at MusesCodeJs in Sydney 07.03.2020]
PDF
Interactive Debugging [Node school Sydney 14.09.19]
PDF
Real-time GraphQL in Angular app
PDF
Real-time GraphQL API with minimum coding and maximum benefit
AI in Robotics: Robocup 2022
State management with GraphQL [Angular Minsk, Online, 13.06.20]
What makes a great engineer [Given at MusesCodeJs in Sydney 07.03.2020]
Interactive Debugging [Node school Sydney 14.09.19]
Real-time GraphQL in Angular app
Real-time GraphQL API with minimum coding and maximum benefit
Ad

Recently uploaded (20)

PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
history of c programming in notes for students .pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
System and Network Administraation Chapter 3
PPTX
Introduction to Artificial Intelligence
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
medical staffing services at VALiNTRY
Operating system designcfffgfgggggggvggggggggg
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
history of c programming in notes for students .pptx
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms II-SECS-1021-03
Design an Analysis of Algorithms I-SECS-1021-03
Odoo POS Development Services by CandidRoot Solutions
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
System and Network Administraation Chapter 3
Introduction to Artificial Intelligence
2025 Textile ERP Trends: SAP, Odoo & Oracle
wealthsignaloriginal-com-DS-text-... (1).pdf
top salesforce developer skills in 2025.pdf
Wondershare Filmora 15 Crack With Activation Key [2025
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Reimagine Home Health with the Power of Agentic AI​
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Softaken Excel to vCard Converter Software.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
medical staffing services at VALiNTRY

Workshop mongo aggregations [musescodejs 16.11.19]