SlideShare a Scribd company logo
MongoDB
Aggregation
Framework
in action !
MongoDB User Group - Nantes
mardi 20 janvier 2015
@sebprunier
/me
Sébastien Prunier
Développeur chez SERLI
@sebprunier
http://blog.sebprunier.com
Sondage (1/2)
?
Sondage (2/2)
?
Aggregation Framework
MongoDB Aggregation Framework
Opérations simples
Map-Reduce
Aggregation Pipeline
Opérations simple
db.collection.count(filter?)
db.collection.distinct(attribute)
db.collection.group(query)
Map-Reduce
db.collection.mapreduce(
mapFunction,
reduceFunction,
options
)
Aggregation Pipeline
db.collection.aggregate(
[
stage1,
stage2,
...,
stageN
]
)
Aggregation Pipeline
json
json
json
json
$match
$unwind
$group
Opérateurs vs. SQL
SQL Terms, Functions, and Concepts MongoDB Aggregation Operators
WHERE $match
GROUP BY $group
HAVING $match
SELECT $project
ORDER BY $sort
LIMIT $limit
SUM() $sum
COUNT() $sum
join N/A
~ $unwind / arrays
Dataset
campings
marvel geeks
Campings
{
"_id" : ObjectId("54bd347880d46d750f7a48c2"),
"raking_date" : ISODate("2013-06-06T22:00:00Z"),
"publication_date" : ISODate("2013-06-06T22:00:00Z"),
"typology" : "CAMPING",
"ranking" : "2 étoiles",
"category" : "Tourisme",
"mention" : "-",
"commercial_name" : "CAMPING LA BERGERIE",
"address" : "4231 route de Giens",
"zip_code" : "83400",
"city" : "HYÈRES",
"phone" : "494589175",
"email" : "info@camping-de-la-bergerie.com",
"website" : "www.camping-de-la-bergerie.com",
"visit_type" : "-",
"capacity" : "-",
"number_of_rooms" : "-",
"number_of_spaces" : "55",
"number_of_housing_units" : "-",
"number_of_chambers" : "-"
}
data.gouv.fr
Campings
“ Déterminer le nombre de campings
pour chaque niveau de classement
(1 étoile, 2 étoiles, etc…) ”
$group
Campings
db.campings.aggregate([
{$group : {_id : "$ranking", total : {$sum : 1}}}
])
{ "_id" : "5 étoiles", "total" : 177 }
{ "_id" : "4 étoiles", "total" : 940 }
{ "_id" : "3 étoiles", "total" : 2224 }
{ "_id" : "2 étoiles", "total" : 1681 }
{ "_id" : "1 étoile", "total" : 389 }
Campings
“ Top 5 des villes avec le plus de
campings ”
$group
$sort $limit
$project
Campings
db.campings.aggregate([
{$group : {_id : "$city", total : {$sum : 1}}},
{$sort : {total : -1}},
{$limit : 5},
{$project: {_id: 0, city : "$_id", total: 1}}
])
{ "city" : "ARGELÈS-SUR-MER" , "total" : 29 }
{ "city" : "AGDE", "total" : 23 }
{ "city" : "VIAS", "total" : 20 }
{ "city" : "SAINT-JEAN-DE-MONTS" , "total" : 20 }
{ "city" : "LES MATHES", "total" : 17 }
Campings
“ Nombre de villes avec seulement un
camping ”
$group
$match
$project
Campings
db.campings.aggregate([
{$group : {_id : "$city", total : {$sum : 1}}},
{$match : {total : 1}},
{$group: {_id: null, count: {$sum: 1 }}},
{$project: {_id: 0, count: 1}}
])
{ "count" : 2802 }
Marvel
{
"_id" : 4,
"title" : "Rogue (2004) #5",
"description" : "...",
"format" : "Comic",
"creators" : {
"available" : 6,
"items" : [...]
},
"characters" : {
"available" : 1,
"items" : [
{
"name" : "Rogue"
}
]
},
...
}
developer.marvel.com
Marvel
“ Top 5 des personnages apparaissant
dans le plus de bandes dessinées ”
$match $project
$unwind
$group
$sort $limit
Marvel
“Deconstructs an array field from the input documents to output a
document for each element.”
$unwind
{
"name" : "john doe",
"tags" : ["A", "B", "C"]
}
{
"name" : "john doe",
"tags" : "A"
}
{
"name" : "john doe",
"tags" : "B"
}
{
"name" : "john doe",
"tags" : "C"
}
$unwind
Marvel
db.comics.aggregate([
{$match : {"characters.returned" : {$gt : 0}}},
{$project : {title : 1, characters : 1}},
{$unwind : "$characters.items"},
{$group : {_id : "$characters.items.name", total : {$sum : 1}}},
{$sort : {total : -1}},
{$limit : 5}
])
{"_id": "Spider-Man","total": 2413}
{"_id": "X-Men","total": 2320}
{"_id": "Iron Man","total": 1904}
{"_id": "Wolverine","total": 1594}
{"_id": "Captain America" ,"total": 1367}
Marvel
“ Créez la collection des personnages à
partir de la collections des bandes
dessinées ”
$match $project
$unwind $group
$out
Marvel
db.comics.aggregate([
{$match : {"characters.returned" : {$gt : 0}}},
{$project : {title : 1, characters : 1}},
{$unwind : "$characters.items"},
{$group : {
_id : "$characters.items.name",
total : {$sum : 1},
comics : {$push : {id : "$_id", title : "$title"}}}
},
{$out : "characters"}
])
Marvel
{
"_id": "Frog-Man",
"total": 2,
"comics": [
{
"id": 38126,
"title": "Spider-Man: New York Stories (Trade Paperback)"
},
{
"id": 39753,
"title": "Spider-Island: Avengers (2011) #1"
}
]
}
Geeks
{
"_id" : ObjectId("54bd3f1a84c2c169160a88d5"),
"nom" : "Mark Zuckerberg",
"ville" : "Palo Alto",
"likes" : [
"Facebook",
"Tongues",
"PHP"
],
"imageUrl" : "static/GIT_HASH/img/geek5.jpg",
"location" : {
"type" : "Point",
"coordinates" : [
-122.1430195,
37.4418834
]
}
}
code-story.net
Geeks
“ Rechercher ce qui rassemble le plus
de geeks autour de Paris ”
$geoNear
$project $unwind
$group $match
$sort
Geeks
db.geeks.aggregate([
{
$geoNear: {
near: {
type: "Point",
coordinates: [2.352241, 48.856638]
},
distanceField: "distance",
spherical: true,
maxDistance: 10000
}
},
...
])
Geeks
{
"_id" : "java",
"total" : 8,
"friends" : [
{
"nom" : "...",
"distance" : 3.015741215085397
},
{
"nom" : "...",
"distance" : 9864.484291991206
},
...
]
}
Index
● Index utilisé pour certains opérateurs, en
début de pipeline
○ $match
○ $sort
○ $geoNear
● explain
○ db.collection.aggregate([...],
{explain: true})
Index
● Optimisations
{ $sort: { age : -1 } },
{ $match: { status: 'A' } }
{ $match: { status: 'A' } },
{ $sort: { age : -1 } }
“ Optimizations are subject to change between releases. “
Sharding
● Découpage du pipeline en deux parties
○ 1ère partie (jusqu’au premier $group ou $sort)
exécutée sur chaque shard (certains shards peuvent
être exclus grâce à un $match)
○ 2ème partie exécutée sur le shard primaire à partir
des résultats consolidés de la première partie
The end
Merci pour votre attention !
● Questions / Réponses
● Partage d’expérience

More Related Content

What's hot (20)

PDF
You Don't Need Lodash
UpsideTravel
 
PDF
PhoneGap: Local Storage
Ivano Malavolta
 
PPTX
Mongo db modifiers
zarigatongy
 
PDF
MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...
MongoDB
 
PPT
JQuery Flot
Arshavski Alexander
 
PDF
HadoopとMongoDBを活用したソーシャルアプリのログ解析
Takahiro Inoue
 
PDF
2013-03-23 - NoSQL Spartakiade
Johannes Hoppe
 
PDF
Groovy scripts with Groovy
Andrés Viedma Peláez
 
PDF
De normalised london aggregation framework overview
Chris Harris
 
PDF
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
 
PDF
Canvas - The Cure
Robert Nyman
 
PDF
NoSQL を Ruby で実践するための n 個の方法
Tomohiro Nishimura
 
PDF
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Luis Curo Salvatierra
 
PDF
Advanced MongoDB #1
Takahiro Inoue
 
PDF
San Francisco Java User Group
kchodorow
 
PDF
Sensmon couchdb
Motokazu Nishimura
 
PDF
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
MongoDB
 
PDF
MongoDB With Style
Gabriele Lana
 
PDF
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
PDF
DataMapper @ RubyEnRails2009
Dirkjan Bussink
 
You Don't Need Lodash
UpsideTravel
 
PhoneGap: Local Storage
Ivano Malavolta
 
Mongo db modifiers
zarigatongy
 
MongoDB Europe 2016 - Enabling the Internet of Things at Proximus - Belgium's...
MongoDB
 
JQuery Flot
Arshavski Alexander
 
HadoopとMongoDBを活用したソーシャルアプリのログ解析
Takahiro Inoue
 
2013-03-23 - NoSQL Spartakiade
Johannes Hoppe
 
Groovy scripts with Groovy
Andrés Viedma Peláez
 
De normalised london aggregation framework overview
Chris Harris
 
From mysql to MongoDB(MongoDB2011北京交流会)
Night Sailer
 
Canvas - The Cure
Robert Nyman
 
NoSQL を Ruby で実践するための n 個の方法
Tomohiro Nishimura
 
Desarrollo de módulos en Drupal e integración con dispositivos móviles
Luis Curo Salvatierra
 
Advanced MongoDB #1
Takahiro Inoue
 
San Francisco Java User Group
kchodorow
 
Sensmon couchdb
Motokazu Nishimura
 
Scaling MongoDB; Sharding Into and Beyond the Multi-Terabyte Range
MongoDB
 
MongoDB With Style
Gabriele Lana
 
MySQL flexible schema and JSON for Internet of Things
Alexander Rubin
 
DataMapper @ RubyEnRails2009
Dirkjan Bussink
 

Viewers also liked (20)

PPTX
The Aggregation Framework
MongoDB
 
PDF
Mongodb Aggregation Pipeline
zahid-mian
 
ODP
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
PPTX
Aggregation Framework
MongoDB
 
PDF
Mongo db aggregation guide
Deysi Gmarra
 
PDF
MongoDB Aggregation Framework
Caserta
 
PDF
ToursJUG mongoDB
Cedric Gatay
 
PPTX
MongoDB Shell Tips & Tricks
MongoDB
 
KEY
MongoDB Aggregation Framework
Tyler Brock
 
PPTX
MongoDB World 2016 : Advanced Aggregation
Joe Drumgoole
 
PDF
Zenika MongoDB Tour - REX Amadeus
François Fornaciari
 
PDF
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
PDF
MongoDB Management & Ansible
MongoDB
 
PPTX
Document validation in MongoDB 3.2
Andrew Morgan
 
PDF
MongoDB day Paris 2012
FastConnect
 
PDF
Dynamic Allocation in Spark
Databricks
 
PPTX
The Aggregation Framework
MongoDB
 
PPTX
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
PPT
Ciencias fisiologicas vision
Grupos de Estudio de Medicina
 
PPT
Cap com2011 communication en ligne-didry
Cap'Com
 
The Aggregation Framework
MongoDB
 
Mongodb Aggregation Pipeline
zahid-mian
 
Aggregation Framework in MongoDB Overview Part-1
Anuj Jain
 
Aggregation Framework
MongoDB
 
Mongo db aggregation guide
Deysi Gmarra
 
MongoDB Aggregation Framework
Caserta
 
ToursJUG mongoDB
Cedric Gatay
 
MongoDB Shell Tips & Tricks
MongoDB
 
MongoDB Aggregation Framework
Tyler Brock
 
MongoDB World 2016 : Advanced Aggregation
Joe Drumgoole
 
Zenika MongoDB Tour - REX Amadeus
François Fornaciari
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Henrik Ingo
 
MongoDB Management & Ansible
MongoDB
 
Document validation in MongoDB 3.2
Andrew Morgan
 
MongoDB day Paris 2012
FastConnect
 
Dynamic Allocation in Spark
Databricks
 
The Aggregation Framework
MongoDB
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB
 
Ciencias fisiologicas vision
Grupos de Estudio de Medicina
 
Cap com2011 communication en ligne-didry
Cap'Com
 
Ad

Similar to MongoDB Aggregation Framework in action ! (20)

PPTX
Introduction to MongoDB for C# developers
Taras Romanyk
 
PDF
Starting out with MongoDB
Harvard Web Working Group
 
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
 
PPTX
Querying mongo db
Bogdan Sabău
 
PDF
MongoDB Analytics
datablend
 
PDF
CouchDB on Rails - FrozenRails 2010
Jonathan Weiss
 
PDF
CouchDB on Rails
Jonathan Weiss
 
PDF
CouchDB on Rails - RailsWayCon 2010
Jonathan Weiss
 
PPTX
Powerful Analysis with the Aggregation Pipeline
MongoDB
 
PDF
Visual Api Training
Spark Summit
 
PDF
Building Apps with MongoDB
Nate Abele
 
PDF
building_games_with_ruby_rubyconf
tutorialsruby
 
PDF
building_games_with_ruby_rubyconf
tutorialsruby
 
PPTX
Mongo db mug_2012-02-07
Will Button
 
PDF
Doing More with MongoDB Aggregation
MongoDB
 
PDF
Mongoskin - Guilin
Jackson Tian
 
PPTX
MongoDB Aggregations Indexing and Profiling
Manish Kapoor
 
PPTX
Joins and Other Aggregation Enhancements Coming in MongoDB 3.2
MongoDB
 
Introduction to MongoDB for C# developers
Taras Romanyk
 
Starting out with MongoDB
Harvard Web Working Group
 
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
 
Querying mongo db
Bogdan Sabău
 
MongoDB Analytics
datablend
 
CouchDB on Rails - FrozenRails 2010
Jonathan Weiss
 
CouchDB on Rails
Jonathan Weiss
 
CouchDB on Rails - RailsWayCon 2010
Jonathan Weiss
 
Powerful Analysis with the Aggregation Pipeline
MongoDB
 
Visual Api Training
Spark Summit
 
Building Apps with MongoDB
Nate Abele
 
building_games_with_ruby_rubyconf
tutorialsruby
 
building_games_with_ruby_rubyconf
tutorialsruby
 
Mongo db mug_2012-02-07
Will Button
 
Doing More with MongoDB Aggregation
MongoDB
 
Mongoskin - Guilin
Jackson Tian
 
MongoDB Aggregations Indexing and Profiling
Manish Kapoor
 
Joins and Other Aggregation Enhancements Coming in MongoDB 3.2
MongoDB
 
Ad

More from Sébastien Prunier (12)

PDF
De votre idée géniale à votre Minimum Viable Product - Café Techno Niort ...
Sébastien Prunier
 
PDF
De votre idée géniale à votre Minimum Viable Product - Rencontres National...
Sébastien Prunier
 
PDF
MongoDB et Elasticsearch, meilleurs ennemis ?
Sébastien Prunier
 
PDF
[Breizhcamp 2015] MongoDB et Elastic, meilleurs ennemis ?
Sébastien Prunier
 
PDF
[Breizhcamp 2015] Refactoring avec 1,22% de code couvert par les tests ... Go...
Sébastien Prunier
 
PDF
Refactoring avec 1,22% de code couvert par les tests ... Golden Master testin...
Sébastien Prunier
 
PDF
Nantes JUG - Les News - 2013-10-10
Sébastien Prunier
 
PDF
JugSummerCamp 2013 - Un backend NoSQL pour Geektic avec MongoDB
Sébastien Prunier
 
PDF
Poitou Charentes JUG - Traçabilité dans une architecture distribuée avec Node...
Sébastien Prunier
 
PPTX
Nantes JUG - Traçabilité dans une architecture distribuée avec Node.js et Mon...
Sébastien Prunier
 
PPTX
Add BPM to your business applications with Bonita Open Solution - JugSummerCa...
Sébastien Prunier
 
PPTX
Nantes Jug - Java 7
Sébastien Prunier
 
De votre idée géniale à votre Minimum Viable Product - Café Techno Niort ...
Sébastien Prunier
 
De votre idée géniale à votre Minimum Viable Product - Rencontres National...
Sébastien Prunier
 
MongoDB et Elasticsearch, meilleurs ennemis ?
Sébastien Prunier
 
[Breizhcamp 2015] MongoDB et Elastic, meilleurs ennemis ?
Sébastien Prunier
 
[Breizhcamp 2015] Refactoring avec 1,22% de code couvert par les tests ... Go...
Sébastien Prunier
 
Refactoring avec 1,22% de code couvert par les tests ... Golden Master testin...
Sébastien Prunier
 
Nantes JUG - Les News - 2013-10-10
Sébastien Prunier
 
JugSummerCamp 2013 - Un backend NoSQL pour Geektic avec MongoDB
Sébastien Prunier
 
Poitou Charentes JUG - Traçabilité dans une architecture distribuée avec Node...
Sébastien Prunier
 
Nantes JUG - Traçabilité dans une architecture distribuée avec Node.js et Mon...
Sébastien Prunier
 
Add BPM to your business applications with Bonita Open Solution - JugSummerCa...
Sébastien Prunier
 
Nantes Jug - Java 7
Sébastien Prunier
 

Recently uploaded (20)

PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Digital Circuits, important subject in CS
contactparinay1
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 

MongoDB Aggregation Framework in action !