SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
Zahid Mian
Part of the Brown-bag Series
Basic Aggregate functions available
Count, Distinct, Group
MongoDB doesn’t support SQL syntax
Aggregation requires building of “pipeline”
Essentially, one step/stage at a time, e.g.:
Step 1: Filter
Step 2: Projection
Step 3: Group
http://docs.mongodb.org/getting-started/shell/import-data/
db.restaurants.count();
> db.restaurants.distinct("borough");
[
"Brooklyn",
"Bronx",
"Manhattan",
"Queens",
"Staten Island",
"Missing"
]
> db.restaurants.group( {
... key: { borough: 1 },
... cond: { cuisine: "Bakery"},
... reduce: function(cur, result) { result.count += 1 },
... initial: { count: 0 }
... } );
[
{
"borough" : "Bronx",
"count" : 71
},
{
"borough" : "Manhattan",
"count" : 221
},
{
"borough" : "Brooklyn",
"count" : 173
},
{
"borough" : "Queens",
"count" : 204
},
{
"borough" : "Staten Island",
"count" : 20
},
{
"borough" : "Missing",
"count" : 2
}
]
>
key is equivalent to the group by clause
cond is equivalent to the where clause
reduce function is called for each document in the
collection that passes the condition
reduce function has two parameters: cur and result. cur
stores the current document and result stores the result so
far for that group
In this case result.count simply adds 1 for each document
initial sets the initial value for each group result
> db.restaurants.count();
25359
> db.restaurants.aggregate([{$group:{_id:'$cuisine', total: {$sum:1}}}]);
{ "_id" : "Chilean", "total" : 1 }
{ "_id" : "Californian", "total" : 1 }
{ "_id" : "Creole/Cajun", "total" : 1 }
{ "_id" : "Hawaiian", "total" : 3 }
{ "_id" : "Nuts/Confectionary", "total" : 6 }
{ "_id" : "Chinese/Japanese", "total" : 59 }
{ "_id" : "Soups", "total" : 4 }
{ "_id" : "Bagels/Pretzels", "total" : 168 }
{ "_id" : "Polynesian", "total" : 1 }
{ "_id" : "Delicatessen", "total" : 321 }
{ "_id" : "Eastern European", "total" : 65 }
{ "_id" : "Scandinavian", "total" : 7 }
{ "_id" : "Afghan", "total" : 14 }
{ "_id" : "Iranian", "total" : 2 }
{ "_id" : "Fruits/Vegetables", "total" : 7 }
{ "_id" : "German", "total" : 31 }
{ "_id" : "Creole", "total" : 24 }
{ "_id" : "Steak", "total" : 86 }
{ "_id" : "Czech", "total" : 6 }
{ "_id" : "Peruvian", "total" : 68 }
Type "it" for more
db.restaurants.aggregate(
[ // bracket indicates an array
{ // first "step" or stage
$group:{ // aggregate operator
_id:'$cuisine', // group by cuisine property
total: {$sum:1} // sum or count each “row”
}
}
]
);
> db.restaurants.aggregate(
... [
... {$group:{_id:'$cuisine', total: {$sum:1}}},
… {$sort: {total:-1}}
... ]
... );
{ "_id" : "American ", "total" : 6183 }
{ "_id" : "Chinese", "total" : 2418 }
{ "_id" : "Café/Coffee/Tea", "total" : 1214 }
{ "_id" : "Pizza", "total" : 1163 }
{ "_id" : "Italian", "total" : 1069 }
{ "_id" : "Other", "total" : 1011 }
{ "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "total" : 850 }
{ "_id" : "Japanese", "total" : 760 }
{ "_id" : "Mexican", "total" : 754 }
{ "_id" : "Bakery", "total" : 691 }
{ "_id" : "Caribbean", "total" : 657 }
{ "_id" : "Spanish", "total" : 637 }
{ "_id" : "Donuts", "total" : 479 }
{ "_id" : "Pizza/Italian", "total" : 468 }
{ "_id" : "Sandwiches", "total" : 459 }
{ "_id" : "Hamburgers", "total" : 433 }
{ "_id" : "Chicken", "total" : 410 }
{ "_id" : "Ice Cream, Gelato,Yogurt, Ices", "total" : 348 }
{ "_id" : "French", "total" : 344 }
{ "_id" : "Delicatessen", "total" : 321 }
Type "it" for more
db.restaurants.aggregate(
[ // bracket indicates an array
{ // first "step" or stage
$group:{ // aggregate operator
_id:'$cuisine', // group by cuisine property
total: {$sum:1} // sum or count each “row”
}
},
{ // second "step" or stage
$sort: { // sort operator
total:-1 // sort on total; -1 indicates DESC
}
}
]
);
> db.restaurants.aggregate(
... [
... {$match : {borough: "Bronx"}},
... {$group:{_id:'$cuisine', total: {$sum:1}}},
... {$sort: {total:-1}}
... ]
... );
{ "_id" : "American ", "total" : 411 }
{ "_id" : "Chinese", "total" : 323 }
{ "_id" : "Pizza", "total" : 197 }
{ "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "total" : 187 }
{ "_id" : "Spanish", "total" : 127 }
{ "_id" : "Caribbean", "total" : 110 }
{ "_id" : "Chicken", "total" : 108 }
{ "_id" : "Mexican", "total" : 89 }
{ "_id" : "Other", "total" : 86 }
{ "_id" : "Hamburgers", "total" : 78 }
{ "_id" : "Bakery", "total" : 71 }
{ "_id" : "Donuts", "total" : 68 }
{ "_id" : "Pizza/Italian", "total" : 53 }
{ "_id" : "Italian", "total" : 52 }
{ "_id" : "Sandwiches", "total" : 49 }
{ "_id" : "Café/Coffee/Tea", "total" : 45 }
{ "_id" : "Juice, Smoothies, Fruit Salads", "total" : 35 }
{ "_id" : "African", "total" : 31 }
{ "_id" : "Ice Cream, Gelato,Yogurt, Ices", "total" : 27 }
{ "_id" : "Seafood", "total" : 26 }
Type "it" for more
db.restaurants.aggregate(
[ // bracket indicates an array
{ // first "step" or stage
$match : { // match operator
borough: "Bronx" // where borough = "Bronx"
}
},
{ // second "step" or stage
$group:{ // aggregate operator
_id:'$cuisine', // group by cuisine property
total: {$sum:1} // sum or count each “row”
}
},
{ // third "step" or stage
$sort: {
total:-1 // sort on total; -1 indicates DESC
}
}
]
);
$sum
$avg
$first
$last
$max
$min
$push
$addToSet: similar to $push, but adds unique
values
Returns an array of all values that result from applying an expression to each document in a group
> db.restaurants.aggregate(
... [
... {
... $group:
... {
... _id: { cuisine: "$cuisine" },
... restaurantByStreet: { $push: { name: "$name" } }
... }
... },
... {$limit: 4},
... {$skip: 3}
... ]
... ).pretty();
{
"_id" : {
"cuisine" : "Hawaiian"
},
"restaurantByStreet" : [
{
"name" : "Makana"
},
{
"name" : "General Assembly"
},
{
"name" : "Onomea"
}
]
}
>
http://docs.mongodb.org/getting-started/shell/import-data/
http://docs.mongodb.org/getting-started/shell/import-data/
http://docs.mongodb.org/getting-started/shell/import-data/
Sort by borough ASC, cuisine DESC
> db.restaurants.aggregate(
... [
... {$group:{_id:{borough: '$borough', cuisine:'$cuisine' }, total: {$sum:1}}},
... {$sort: {"_id.borough":1, "_id.cuisine":-1}}, // use dot notation
... {$limit: 5 }
... ]
... );
{ "_id" : { "borough" : "Bronx", "cuisine" : "Thai" }, "total" : 2 }
{ "_id" : { "borough" : "Bronx", "cuisine" : "Tex-Mex" }, "total" : 11 }
{ "_id" : { "borough" : "Bronx", "cuisine" : "Steak" }, "total" : 4 }
{ "_id" : { "borough" : "Bronx", "cuisine" : "Spanish" }, "total" : 127 }
{ "_id" : { "borough" : "Bronx", "cuisine" : "Soups & Sandwiches" }, "total" : 1 }
>
Controls which values are output
> db.restaurants.aggregate(
... [
... {$limit:1},
... {$project: {_id:0, // hide the _id value
… restaurant_id:1, // show restaurant_id
… "restaurant_name":"$name", // rename/alias name to restaurant_name
… "grades.grade":1}} // show grades.grade
... ]).pretty();
{
"grades" : [
{
"grade" : "A" // part of output
},
{
"grade" : "B" // part of output
},
{
"grade" : "A" // part of output
},
{
"grade" : "A" // part of output
}
],
"restaurant_name" : "Wendy'S", // part of output; renamed
"restaurant_id" : "30112340" // part of output
}
>
Saves the output of a pipeline to a collection
> db.restaurants.aggregate(
... [
... {$match : {borough: "Bronx"}},
... {$group:{_id:'$cuisine', total: {$sum:1}}},
... {$sort: {total:-1}},
... {$limit: 5 },
... {$out: "top5"} // output data to a collection called top5
... ]
... );
> db.top5.find({}); // retrieve all data from top5
{ "_id" : "American ", "total" : 411 }
{ "_id" : "Chinese", "total" : 323 }
{ "_id" : "Pizza", "total" : 197 }
{ "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central
American)", "total" : 187 }
{ "_id" : "Spanish", "total" : 127 }
>
Motivation: How many A grades
did a restaurant get?
> db.restaurants.find({_id: ObjectId("5602b9200a67e499361c05ad")}).pretty();
{
"_id" : ObjectId("5602b9200a67e499361c05ad"),
"address" : {
"street" : "Flatbush Avenue",
"zipcode" : "11225",
"building" : "469",
"coord" : [
-73.961704,
40.662942
]
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [ // this is an array of objects
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A", // A grade
"score" : 8
},
{
"grade" : "B", // B grade
"score" : 23,
"date" : ISODate("2014-07-01T00:00:00Z")
},
{
"score" : 12,
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A"
},
{
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
>
Basic pipeline
Stage 1: unwind grades
Stage 2: match grade of
“A”
Stage 3: group by / sum
Stage 4: project (alias)
There is only one document for that restaurant_id, but since there were 4 elements in
grades, the unwind operator created 4 documents, one for each grade
Notice the result of the following is four documents with the same restaurant_id
> db.restaurants.aggregate(
... [
... {$unwind: "$grades"}, // unwind the grades array
... {$limit:4}, // limit the output to 4 documents
... {$project: {_id:0, restaurant_id:1, "grades.date":1, "grades.grade":1, "grades.score":1}}
... ]).pretty();
{
"grades" : {
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
"restaurant_id": "30112340"
}
{
"grades" : {
"grade" : "B",
"score" : 23,
"date" : ISODate("2014-07-01T00:00:00Z")
},
"restaurant_id": "30112340"
}
{
"grades" : {
"score" : 12,
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A"
},
"restaurant_id": "30112340"
}
{
"grades" : {
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
},
"restaurant_id": "30112340"
}
> db.restaurants.aggregate(
... [
... {$unwind: "$grades"},
... {$project: {_id:0, restaurant_id:1, name:1, "grades.grade":1}},
... {$match: {"grades.grade":"A"} }, // only count A grades
... {$group: {_id:{restaurant_id:'$restaurant_id', name:'$name' }, total: {$sum:1}}},
... {$sort: {total: -1}},
... {$limit: 5},
… // alias output to get nicer printout
... {$project: {_id:0, "rid":"$_id.restaurant_id", "rname":"$_id.name", total:1}}
... ]).pretty();
{ "total" : 8, "rid" : "41382858", "rname" : "TacoVeloz" }
{ "total" : 7, "rid" : "41587378", "rname" : "Lobster Joint" }
{"total" : 7, "rid" : "41611381", "rname" : "Burger King, Popeye'S Chicken & Biscuits"}
{ "total" : 7, "rid" : "41572121", "rname" : "Luke'S Pizza" }
{ "total" : 7, "rid" : "41578481", "rname" : "Top Hot Bagels & Grill" }
>
Mongodb Aggregation Pipeline

Mais conteúdo relacionado

Mais procurados

Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsVikash Singh
 
Structured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin CoroutinesStructured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin CoroutinesVadims Savjolovs
 
Design applicatif avec symfony2
Design applicatif avec symfony2Design applicatif avec symfony2
Design applicatif avec symfony2RomainKuzniak
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesAltinity Ltd
 
JSON-LD: JSON for Linked Data
JSON-LD: JSON for Linked DataJSON-LD: JSON for Linked Data
JSON-LD: JSON for Linked DataGregg Kellogg
 
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Ontico
 
File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]Katy Slemon
 
Webinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDBWebinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDBMongoDB
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass SlidesNir Kaufman
 
NestJS - O framework progressivo
NestJS - O framework progressivoNestJS - O framework progressivo
NestJS - O framework progressivoWender Machado
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to ChefKnoldus Inc.
 

Mais procurados (20)

Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Angular modules in depth
Angular modules in depthAngular modules in depth
Angular modules in depth
 
Structured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin CoroutinesStructured concurrency with Kotlin Coroutines
Structured concurrency with Kotlin Coroutines
 
Design applicatif avec symfony2
Design applicatif avec symfony2Design applicatif avec symfony2
Design applicatif avec symfony2
 
Express JS
Express JSExpress JS
Express JS
 
JSON-LD and MongoDB
JSON-LD and MongoDBJSON-LD and MongoDB
JSON-LD and MongoDB
 
Introduction to Redis
Introduction to RedisIntroduction to Redis
Introduction to Redis
 
ClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic ContinuesClickHouse Materialized Views: The Magic Continues
ClickHouse Materialized Views: The Magic Continues
 
JSON-LD: JSON for Linked Data
JSON-LD: JSON for Linked DataJSON-LD: JSON for Linked Data
JSON-LD: JSON for Linked Data
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
 
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)Postgres vs Mongo / Олег Бартунов (Postgres Professional)
Postgres vs Mongo / Олег Бартунов (Postgres Professional)
 
File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]File upload using multer in node.js and express.js [2021 tutorial]
File upload using multer in node.js and express.js [2021 tutorial]
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Express node js
Express node jsExpress node js
Express node js
 
Angular
AngularAngular
Angular
 
PythonOOP
PythonOOPPythonOOP
PythonOOP
 
Webinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDBWebinar: Working with Graph Data in MongoDB
Webinar: Working with Graph Data in MongoDB
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
NestJS - O framework progressivo
NestJS - O framework progressivoNestJS - O framework progressivo
NestJS - O framework progressivo
 
Introduction to Chef
Introduction to ChefIntroduction to Chef
Introduction to Chef
 

Destaque

MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation FrameworkTyler Brock
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDBKishor Parkhe
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Anuj Jain
 
Aggregation Framework
Aggregation FrameworkAggregation Framework
Aggregation FrameworkMongoDB
 
Mongo db aggregation guide
Mongo db aggregation guideMongo db aggregation guide
Mongo db aggregation guideDeysi Gmarra
 
MongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced AggregationMongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced AggregationJoe Drumgoole
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichNorberto Leite
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorHenrik Ingo
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation FrameworkMongoDB
 
MongoDB Aggregation Framework in action !
MongoDB Aggregation Framework in action !MongoDB Aggregation Framework in action !
MongoDB Aggregation Framework in action !Sébastien Prunier
 

Destaque (11)

MongoDB Aggregation Framework
MongoDB Aggregation FrameworkMongoDB Aggregation Framework
MongoDB Aggregation Framework
 
Aggregation in MongoDB
Aggregation in MongoDBAggregation in MongoDB
Aggregation in MongoDB
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1Aggregation Framework in MongoDB Overview Part-1
Aggregation Framework in MongoDB Overview Part-1
 
Aggregation Framework
Aggregation FrameworkAggregation Framework
Aggregation Framework
 
Mongo db aggregation guide
Mongo db aggregation guideMongo db aggregation guide
Mongo db aggregation guide
 
MongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced AggregationMongoDB World 2016 : Advanced Aggregation
MongoDB World 2016 : Advanced Aggregation
 
Aggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days MunichAggregation Framework MongoDB Days Munich
Aggregation Framework MongoDB Days Munich
 
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop ConnectorAnalytics with MongoDB Aggregation Framework and Hadoop Connector
Analytics with MongoDB Aggregation Framework and Hadoop Connector
 
The Aggregation Framework
The Aggregation FrameworkThe Aggregation Framework
The Aggregation Framework
 
MongoDB Aggregation Framework in action !
MongoDB Aggregation Framework in action !MongoDB Aggregation Framework in action !
MongoDB Aggregation Framework in action !
 

Semelhante a Mongodb Aggregation Pipeline

Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORA
Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORAComment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORA
Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORALINAGORA
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB
 
Angular.js Fundamentals
Angular.js FundamentalsAngular.js Fundamentals
Angular.js FundamentalsMark
 
Working with web_services
Working with web_servicesWorking with web_services
Working with web_servicesLorna Mitchell
 
Joins and Other MongoDB 3.2 Aggregation Enhancements
Joins and Other MongoDB 3.2 Aggregation EnhancementsJoins and Other MongoDB 3.2 Aggregation Enhancements
Joins and Other MongoDB 3.2 Aggregation EnhancementsAndrew Morgan
 
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & AggregationWebinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & AggregationMongoDB
 
Crash Course to SQL in PHP
Crash Course to SQL in PHPCrash Course to SQL in PHP
Crash Course to SQL in PHPwebhostingguy
 
Powerful Analysis with the Aggregation Pipeline
Powerful Analysis with the Aggregation PipelinePowerful Analysis with the Aggregation Pipeline
Powerful Analysis with the Aggregation PipelineMongoDB
 
Building Glassware with the Glass Development Kit
Building Glassware with the Glass Development KitBuilding Glassware with the Glass Development Kit
Building Glassware with the Glass Development KitEveryware Technologies
 
Riak 2.0 : For Beginners, and Everyone Else
Riak 2.0 : For Beginners, and Everyone ElseRiak 2.0 : For Beginners, and Everyone Else
Riak 2.0 : For Beginners, and Everyone ElseEngin Yoeyen
 
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze..."Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...Mateusz Zalewski
 
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"DataStax Academy
 
Intravert Server side processing for Cassandra
Intravert Server side processing for CassandraIntravert Server side processing for Cassandra
Intravert Server side processing for CassandraEdward Capriolo
 
Hello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdfHello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdffedosys
 
MongoDB Analytics
MongoDB AnalyticsMongoDB Analytics
MongoDB Analyticsdatablend
 
Perkenalan ReasonML
Perkenalan ReasonMLPerkenalan ReasonML
Perkenalan ReasonMLRiza Fahmi
 
Programación de C++, Función Case
Programación de C++, Función CaseProgramación de C++, Función Case
Programación de C++, Función CaseRamon Lop-Mi
 

Semelhante a Mongodb Aggregation Pipeline (20)

Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORA
Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORAComment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORA
Comment faire ses mappings ElasticSearch aux petits oignons ? - LINAGORA
 
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
MongoDB Analytics: Learn Aggregation by Example - Exploratory Analytics and V...
 
Groovy kind of test
Groovy kind of testGroovy kind of test
Groovy kind of test
 
Groovy kind of test
Groovy kind of testGroovy kind of test
Groovy kind of test
 
Angular.js Fundamentals
Angular.js FundamentalsAngular.js Fundamentals
Angular.js Fundamentals
 
Working with web_services
Working with web_servicesWorking with web_services
Working with web_services
 
Joins and Other MongoDB 3.2 Aggregation Enhancements
Joins and Other MongoDB 3.2 Aggregation EnhancementsJoins and Other MongoDB 3.2 Aggregation Enhancements
Joins and Other MongoDB 3.2 Aggregation Enhancements
 
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & AggregationWebinar: Applikationsentwicklung mit MongoDB: Teil 5: Reporting & Aggregation
Webinar: Applikationsentwicklung mit MongoDB : Teil 5: Reporting & Aggregation
 
Crash Course to SQL in PHP
Crash Course to SQL in PHPCrash Course to SQL in PHP
Crash Course to SQL in PHP
 
Powerful Analysis with the Aggregation Pipeline
Powerful Analysis with the Aggregation PipelinePowerful Analysis with the Aggregation Pipeline
Powerful Analysis with the Aggregation Pipeline
 
Building Glassware with the Glass Development Kit
Building Glassware with the Glass Development KitBuilding Glassware with the Glass Development Kit
Building Glassware with the Glass Development Kit
 
Riak 2.0 : For Beginners, and Everyone Else
Riak 2.0 : For Beginners, and Everyone ElseRiak 2.0 : For Beginners, and Everyone Else
Riak 2.0 : For Beginners, and Everyone Else
 
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze..."Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
"Kto to pisał?!... A, to ja.", czyli sposoby, żeby znienawidzić siebie z prze...
 
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
NYC* 2013 - "Advanced Data Processing: Beyond Queries and Slices"
 
Intravert Server side processing for Cassandra
Intravert Server side processing for CassandraIntravert Server side processing for Cassandra
Intravert Server side processing for Cassandra
 
project
projectproject
project
 
Hello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdfHello everyone,Im actually working on a fast food order program..pdf
Hello everyone,Im actually working on a fast food order program..pdf
 
MongoDB Analytics
MongoDB AnalyticsMongoDB Analytics
MongoDB Analytics
 
Perkenalan ReasonML
Perkenalan ReasonMLPerkenalan ReasonML
Perkenalan ReasonML
 
Programación de C++, Función Case
Programación de C++, Función CaseProgramación de C++, Función Case
Programación de C++, Función Case
 

Mais de zahid-mian

MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentialszahid-mian
 
Hadoop Technologies
Hadoop TechnologiesHadoop Technologies
Hadoop Technologieszahid-mian
 
Intro to modern cryptography
Intro to modern cryptographyIntro to modern cryptography
Intro to modern cryptographyzahid-mian
 
Hadoop M/R Pig Hive
Hadoop M/R Pig HiveHadoop M/R Pig Hive
Hadoop M/R Pig Hivezahid-mian
 
NoSQL Databases
NoSQL DatabasesNoSQL Databases
NoSQL Databaseszahid-mian
 
Statistics101: Numerical Measures
Statistics101: Numerical MeasuresStatistics101: Numerical Measures
Statistics101: Numerical Measureszahid-mian
 
Amazon SimpleDB
Amazon SimpleDBAmazon SimpleDB
Amazon SimpleDBzahid-mian
 
C# 6 New Features
C# 6 New FeaturesC# 6 New Features
C# 6 New Featureszahid-mian
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)zahid-mian
 

Mais de zahid-mian (9)

MongoD Essentials
MongoD EssentialsMongoD Essentials
MongoD Essentials
 
Hadoop Technologies
Hadoop TechnologiesHadoop Technologies
Hadoop Technologies
 
Intro to modern cryptography
Intro to modern cryptographyIntro to modern cryptography
Intro to modern cryptography
 
Hadoop M/R Pig Hive
Hadoop M/R Pig HiveHadoop M/R Pig Hive
Hadoop M/R Pig Hive
 
NoSQL Databases
NoSQL DatabasesNoSQL Databases
NoSQL Databases
 
Statistics101: Numerical Measures
Statistics101: Numerical MeasuresStatistics101: Numerical Measures
Statistics101: Numerical Measures
 
Amazon SimpleDB
Amazon SimpleDBAmazon SimpleDB
Amazon SimpleDB
 
C# 6 New Features
C# 6 New FeaturesC# 6 New Features
C# 6 New Features
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)
 

Último

The market for cross-border mortgages in Europe
The market for cross-border mortgages in EuropeThe market for cross-border mortgages in Europe
The market for cross-border mortgages in Europe321k
 
Data Collection from Social Media Platforms
Data Collection from Social Media PlatformsData Collection from Social Media Platforms
Data Collection from Social Media PlatformsMahmoud Yasser
 
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdf
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdfNeo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdf
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdfNeo4j
 
TCFPro24 Building Real-Time Generative AI Pipelines
TCFPro24 Building Real-Time Generative AI PipelinesTCFPro24 Building Real-Time Generative AI Pipelines
TCFPro24 Building Real-Time Generative AI PipelinesTimothy Spann
 
Báo cáo Social Media Benchmark 2024 cho dân Marketing
Báo cáo Social Media Benchmark 2024 cho dân MarketingBáo cáo Social Media Benchmark 2024 cho dân Marketing
Báo cáo Social Media Benchmark 2024 cho dân MarketingMarketingTrips
 
Brain Tumor Detection with Machine Learning.pptx
Brain Tumor Detection with Machine Learning.pptxBrain Tumor Detection with Machine Learning.pptx
Brain Tumor Detection with Machine Learning.pptxShammiRai3
 
STOCK PRICE ANALYSIS Furkan Ali TASCI --.pptx
STOCK PRICE ANALYSIS  Furkan Ali TASCI --.pptxSTOCK PRICE ANALYSIS  Furkan Ali TASCI --.pptx
STOCK PRICE ANALYSIS Furkan Ali TASCI --.pptxFurkanTasci3
 
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...ferisulianta.com
 
2024 Build Generative AI for Non-Profits
2024 Build Generative AI for Non-Profits2024 Build Generative AI for Non-Profits
2024 Build Generative AI for Non-ProfitsTimothy Spann
 
Paul Martin (Gartner) - Show Me the AI Money.pdf
Paul Martin (Gartner) - Show Me the AI Money.pdfPaul Martin (Gartner) - Show Me the AI Money.pdf
Paul Martin (Gartner) - Show Me the AI Money.pdfdcphostmaster
 
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdf
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdfNeo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdf
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdfNeo4j
 
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...Neo4j
 
Empowering Decisions A Guide to Embedded Analytics
Empowering Decisions A Guide to Embedded AnalyticsEmpowering Decisions A Guide to Embedded Analytics
Empowering Decisions A Guide to Embedded AnalyticsGain Insights
 
Unleashing Datas Potential - Mastering Precision with FCO-IM
Unleashing Datas Potential - Mastering Precision with FCO-IMUnleashing Datas Potential - Mastering Precision with FCO-IM
Unleashing Datas Potential - Mastering Precision with FCO-IMMarco Wobben
 
Using DAX & Time-based Analysis in Data Warehouse
Using DAX & Time-based Analysis in Data WarehouseUsing DAX & Time-based Analysis in Data Warehouse
Using DAX & Time-based Analysis in Data WarehouseThinkInnovation
 
Data Analytics Fundamentals: data analytics types.potx
Data Analytics Fundamentals: data analytics types.potxData Analytics Fundamentals: data analytics types.potx
Data Analytics Fundamentals: data analytics types.potxEmmanuel Dauda
 
Microeconomic Group Presentation Apple.pdf
Microeconomic Group Presentation Apple.pdfMicroeconomic Group Presentation Apple.pdf
Microeconomic Group Presentation Apple.pdfmxlos0
 
Air Con Energy Rating Info411 Presentation.pdf
Air Con Energy Rating Info411 Presentation.pdfAir Con Energy Rating Info411 Presentation.pdf
Air Con Energy Rating Info411 Presentation.pdfJasonBoboKyaw
 
Enabling GenAI Breakthroughs with Knowledge Graphs
Enabling GenAI Breakthroughs with Knowledge GraphsEnabling GenAI Breakthroughs with Knowledge Graphs
Enabling GenAI Breakthroughs with Knowledge GraphsNeo4j
 

Último (20)

The market for cross-border mortgages in Europe
The market for cross-border mortgages in EuropeThe market for cross-border mortgages in Europe
The market for cross-border mortgages in Europe
 
Data Collection from Social Media Platforms
Data Collection from Social Media PlatformsData Collection from Social Media Platforms
Data Collection from Social Media Platforms
 
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdf
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdfNeo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdf
Neo4j Graph Summit 2024 Workshop - EMEA - Breda_and_Munchen.pdf
 
TCFPro24 Building Real-Time Generative AI Pipelines
TCFPro24 Building Real-Time Generative AI PipelinesTCFPro24 Building Real-Time Generative AI Pipelines
TCFPro24 Building Real-Time Generative AI Pipelines
 
Báo cáo Social Media Benchmark 2024 cho dân Marketing
Báo cáo Social Media Benchmark 2024 cho dân MarketingBáo cáo Social Media Benchmark 2024 cho dân Marketing
Báo cáo Social Media Benchmark 2024 cho dân Marketing
 
Brain Tumor Detection with Machine Learning.pptx
Brain Tumor Detection with Machine Learning.pptxBrain Tumor Detection with Machine Learning.pptx
Brain Tumor Detection with Machine Learning.pptx
 
Target_Company_Data_breach_2013_110million
Target_Company_Data_breach_2013_110millionTarget_Company_Data_breach_2013_110million
Target_Company_Data_breach_2013_110million
 
STOCK PRICE ANALYSIS Furkan Ali TASCI --.pptx
STOCK PRICE ANALYSIS  Furkan Ali TASCI --.pptxSTOCK PRICE ANALYSIS  Furkan Ali TASCI --.pptx
STOCK PRICE ANALYSIS Furkan Ali TASCI --.pptx
 
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...
Prediction Of Cryptocurrency Prices Using Lstm, Svm And Polynomial Regression...
 
2024 Build Generative AI for Non-Profits
2024 Build Generative AI for Non-Profits2024 Build Generative AI for Non-Profits
2024 Build Generative AI for Non-Profits
 
Paul Martin (Gartner) - Show Me the AI Money.pdf
Paul Martin (Gartner) - Show Me the AI Money.pdfPaul Martin (Gartner) - Show Me the AI Money.pdf
Paul Martin (Gartner) - Show Me the AI Money.pdf
 
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdf
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdfNeo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdf
Neo4j_Jesus Barrasa_The Art of the Possible with Graph.pptx.pdf
 
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...
Deloitte+RedCross_Talk to your data with Knowledge-enriched Generative AI.ppt...
 
Empowering Decisions A Guide to Embedded Analytics
Empowering Decisions A Guide to Embedded AnalyticsEmpowering Decisions A Guide to Embedded Analytics
Empowering Decisions A Guide to Embedded Analytics
 
Unleashing Datas Potential - Mastering Precision with FCO-IM
Unleashing Datas Potential - Mastering Precision with FCO-IMUnleashing Datas Potential - Mastering Precision with FCO-IM
Unleashing Datas Potential - Mastering Precision with FCO-IM
 
Using DAX & Time-based Analysis in Data Warehouse
Using DAX & Time-based Analysis in Data WarehouseUsing DAX & Time-based Analysis in Data Warehouse
Using DAX & Time-based Analysis in Data Warehouse
 
Data Analytics Fundamentals: data analytics types.potx
Data Analytics Fundamentals: data analytics types.potxData Analytics Fundamentals: data analytics types.potx
Data Analytics Fundamentals: data analytics types.potx
 
Microeconomic Group Presentation Apple.pdf
Microeconomic Group Presentation Apple.pdfMicroeconomic Group Presentation Apple.pdf
Microeconomic Group Presentation Apple.pdf
 
Air Con Energy Rating Info411 Presentation.pdf
Air Con Energy Rating Info411 Presentation.pdfAir Con Energy Rating Info411 Presentation.pdf
Air Con Energy Rating Info411 Presentation.pdf
 
Enabling GenAI Breakthroughs with Knowledge Graphs
Enabling GenAI Breakthroughs with Knowledge GraphsEnabling GenAI Breakthroughs with Knowledge Graphs
Enabling GenAI Breakthroughs with Knowledge Graphs
 

Mongodb Aggregation Pipeline

  • 1. Zahid Mian Part of the Brown-bag Series
  • 2. Basic Aggregate functions available Count, Distinct, Group MongoDB doesn’t support SQL syntax Aggregation requires building of “pipeline” Essentially, one step/stage at a time, e.g.: Step 1: Filter Step 2: Projection Step 3: Group
  • 5. > db.restaurants.group( { ... key: { borough: 1 }, ... cond: { cuisine: "Bakery"}, ... reduce: function(cur, result) { result.count += 1 }, ... initial: { count: 0 } ... } ); [ { "borough" : "Bronx", "count" : 71 }, { "borough" : "Manhattan", "count" : 221 }, { "borough" : "Brooklyn", "count" : 173 }, { "borough" : "Queens", "count" : 204 }, { "borough" : "Staten Island", "count" : 20 }, { "borough" : "Missing", "count" : 2 } ] > key is equivalent to the group by clause cond is equivalent to the where clause reduce function is called for each document in the collection that passes the condition reduce function has two parameters: cur and result. cur stores the current document and result stores the result so far for that group In this case result.count simply adds 1 for each document initial sets the initial value for each group result
  • 6. > db.restaurants.count(); 25359 > db.restaurants.aggregate([{$group:{_id:'$cuisine', total: {$sum:1}}}]); { "_id" : "Chilean", "total" : 1 } { "_id" : "Californian", "total" : 1 } { "_id" : "Creole/Cajun", "total" : 1 } { "_id" : "Hawaiian", "total" : 3 } { "_id" : "Nuts/Confectionary", "total" : 6 } { "_id" : "Chinese/Japanese", "total" : 59 } { "_id" : "Soups", "total" : 4 } { "_id" : "Bagels/Pretzels", "total" : 168 } { "_id" : "Polynesian", "total" : 1 } { "_id" : "Delicatessen", "total" : 321 } { "_id" : "Eastern European", "total" : 65 } { "_id" : "Scandinavian", "total" : 7 } { "_id" : "Afghan", "total" : 14 } { "_id" : "Iranian", "total" : 2 } { "_id" : "Fruits/Vegetables", "total" : 7 } { "_id" : "German", "total" : 31 } { "_id" : "Creole", "total" : 24 } { "_id" : "Steak", "total" : 86 } { "_id" : "Czech", "total" : 6 } { "_id" : "Peruvian", "total" : 68 } Type "it" for more
  • 7. db.restaurants.aggregate( [ // bracket indicates an array { // first "step" or stage $group:{ // aggregate operator _id:'$cuisine', // group by cuisine property total: {$sum:1} // sum or count each “row” } } ] );
  • 8. > db.restaurants.aggregate( ... [ ... {$group:{_id:'$cuisine', total: {$sum:1}}}, … {$sort: {total:-1}} ... ] ... ); { "_id" : "American ", "total" : 6183 } { "_id" : "Chinese", "total" : 2418 } { "_id" : "Café/Coffee/Tea", "total" : 1214 } { "_id" : "Pizza", "total" : 1163 } { "_id" : "Italian", "total" : 1069 } { "_id" : "Other", "total" : 1011 } { "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "total" : 850 } { "_id" : "Japanese", "total" : 760 } { "_id" : "Mexican", "total" : 754 } { "_id" : "Bakery", "total" : 691 } { "_id" : "Caribbean", "total" : 657 } { "_id" : "Spanish", "total" : 637 } { "_id" : "Donuts", "total" : 479 } { "_id" : "Pizza/Italian", "total" : 468 } { "_id" : "Sandwiches", "total" : 459 } { "_id" : "Hamburgers", "total" : 433 } { "_id" : "Chicken", "total" : 410 } { "_id" : "Ice Cream, Gelato,Yogurt, Ices", "total" : 348 } { "_id" : "French", "total" : 344 } { "_id" : "Delicatessen", "total" : 321 } Type "it" for more
  • 9. db.restaurants.aggregate( [ // bracket indicates an array { // first "step" or stage $group:{ // aggregate operator _id:'$cuisine', // group by cuisine property total: {$sum:1} // sum or count each “row” } }, { // second "step" or stage $sort: { // sort operator total:-1 // sort on total; -1 indicates DESC } } ] );
  • 10. > db.restaurants.aggregate( ... [ ... {$match : {borough: "Bronx"}}, ... {$group:{_id:'$cuisine', total: {$sum:1}}}, ... {$sort: {total:-1}} ... ] ... ); { "_id" : "American ", "total" : 411 } { "_id" : "Chinese", "total" : 323 } { "_id" : "Pizza", "total" : 197 } { "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "total" : 187 } { "_id" : "Spanish", "total" : 127 } { "_id" : "Caribbean", "total" : 110 } { "_id" : "Chicken", "total" : 108 } { "_id" : "Mexican", "total" : 89 } { "_id" : "Other", "total" : 86 } { "_id" : "Hamburgers", "total" : 78 } { "_id" : "Bakery", "total" : 71 } { "_id" : "Donuts", "total" : 68 } { "_id" : "Pizza/Italian", "total" : 53 } { "_id" : "Italian", "total" : 52 } { "_id" : "Sandwiches", "total" : 49 } { "_id" : "Café/Coffee/Tea", "total" : 45 } { "_id" : "Juice, Smoothies, Fruit Salads", "total" : 35 } { "_id" : "African", "total" : 31 } { "_id" : "Ice Cream, Gelato,Yogurt, Ices", "total" : 27 } { "_id" : "Seafood", "total" : 26 } Type "it" for more
  • 11. db.restaurants.aggregate( [ // bracket indicates an array { // first "step" or stage $match : { // match operator borough: "Bronx" // where borough = "Bronx" } }, { // second "step" or stage $group:{ // aggregate operator _id:'$cuisine', // group by cuisine property total: {$sum:1} // sum or count each “row” } }, { // third "step" or stage $sort: { total:-1 // sort on total; -1 indicates DESC } } ] );
  • 13. Returns an array of all values that result from applying an expression to each document in a group > db.restaurants.aggregate( ... [ ... { ... $group: ... { ... _id: { cuisine: "$cuisine" }, ... restaurantByStreet: { $push: { name: "$name" } } ... } ... }, ... {$limit: 4}, ... {$skip: 3} ... ] ... ).pretty(); { "_id" : { "cuisine" : "Hawaiian" }, "restaurantByStreet" : [ { "name" : "Makana" }, { "name" : "General Assembly" }, { "name" : "Onomea" } ] } >
  • 17. Sort by borough ASC, cuisine DESC > db.restaurants.aggregate( ... [ ... {$group:{_id:{borough: '$borough', cuisine:'$cuisine' }, total: {$sum:1}}}, ... {$sort: {"_id.borough":1, "_id.cuisine":-1}}, // use dot notation ... {$limit: 5 } ... ] ... ); { "_id" : { "borough" : "Bronx", "cuisine" : "Thai" }, "total" : 2 } { "_id" : { "borough" : "Bronx", "cuisine" : "Tex-Mex" }, "total" : 11 } { "_id" : { "borough" : "Bronx", "cuisine" : "Steak" }, "total" : 4 } { "_id" : { "borough" : "Bronx", "cuisine" : "Spanish" }, "total" : 127 } { "_id" : { "borough" : "Bronx", "cuisine" : "Soups & Sandwiches" }, "total" : 1 } >
  • 18. Controls which values are output > db.restaurants.aggregate( ... [ ... {$limit:1}, ... {$project: {_id:0, // hide the _id value … restaurant_id:1, // show restaurant_id … "restaurant_name":"$name", // rename/alias name to restaurant_name … "grades.grade":1}} // show grades.grade ... ]).pretty(); { "grades" : [ { "grade" : "A" // part of output }, { "grade" : "B" // part of output }, { "grade" : "A" // part of output }, { "grade" : "A" // part of output } ], "restaurant_name" : "Wendy'S", // part of output; renamed "restaurant_id" : "30112340" // part of output } >
  • 19. Saves the output of a pipeline to a collection > db.restaurants.aggregate( ... [ ... {$match : {borough: "Bronx"}}, ... {$group:{_id:'$cuisine', total: {$sum:1}}}, ... {$sort: {total:-1}}, ... {$limit: 5 }, ... {$out: "top5"} // output data to a collection called top5 ... ] ... ); > db.top5.find({}); // retrieve all data from top5 { "_id" : "American ", "total" : 411 } { "_id" : "Chinese", "total" : 323 } { "_id" : "Pizza", "total" : 197 } { "_id" : "Latin (Cuban, Dominican, Puerto Rican, South & Central American)", "total" : 187 } { "_id" : "Spanish", "total" : 127 } >
  • 20. Motivation: How many A grades did a restaurant get? > db.restaurants.find({_id: ObjectId("5602b9200a67e499361c05ad")}).pretty(); { "_id" : ObjectId("5602b9200a67e499361c05ad"), "address" : { "street" : "Flatbush Avenue", "zipcode" : "11225", "building" : "469", "coord" : [ -73.961704, 40.662942 ] }, "borough" : "Brooklyn", "cuisine" : "Hamburgers", "grades" : [ // this is an array of objects { "date" : ISODate("2014-12-30T00:00:00Z"), "grade" : "A", // A grade "score" : 8 }, { "grade" : "B", // B grade "score" : 23, "date" : ISODate("2014-07-01T00:00:00Z") }, { "score" : 12, "date" : ISODate("2013-04-30T00:00:00Z"), "grade" : "A" }, { "date" : ISODate("2012-05-08T00:00:00Z"), "grade" : "A", "score" : 12 } ], "name" : "Wendy'S", "restaurant_id" : "30112340" } > Basic pipeline Stage 1: unwind grades Stage 2: match grade of “A” Stage 3: group by / sum Stage 4: project (alias)
  • 21. There is only one document for that restaurant_id, but since there were 4 elements in grades, the unwind operator created 4 documents, one for each grade Notice the result of the following is four documents with the same restaurant_id > db.restaurants.aggregate( ... [ ... {$unwind: "$grades"}, // unwind the grades array ... {$limit:4}, // limit the output to 4 documents ... {$project: {_id:0, restaurant_id:1, "grades.date":1, "grades.grade":1, "grades.score":1}} ... ]).pretty(); { "grades" : { "date" : ISODate("2014-12-30T00:00:00Z"), "grade" : "A", "score" : 8 }, "restaurant_id": "30112340" } { "grades" : { "grade" : "B", "score" : 23, "date" : ISODate("2014-07-01T00:00:00Z") }, "restaurant_id": "30112340" } { "grades" : { "score" : 12, "date" : ISODate("2013-04-30T00:00:00Z"), "grade" : "A" }, "restaurant_id": "30112340" } { "grades" : { "date" : ISODate("2012-05-08T00:00:00Z"), "grade" : "A", "score" : 12 }, "restaurant_id": "30112340" }
  • 22. > db.restaurants.aggregate( ... [ ... {$unwind: "$grades"}, ... {$project: {_id:0, restaurant_id:1, name:1, "grades.grade":1}}, ... {$match: {"grades.grade":"A"} }, // only count A grades ... {$group: {_id:{restaurant_id:'$restaurant_id', name:'$name' }, total: {$sum:1}}}, ... {$sort: {total: -1}}, ... {$limit: 5}, … // alias output to get nicer printout ... {$project: {_id:0, "rid":"$_id.restaurant_id", "rname":"$_id.name", total:1}} ... ]).pretty(); { "total" : 8, "rid" : "41382858", "rname" : "TacoVeloz" } { "total" : 7, "rid" : "41587378", "rname" : "Lobster Joint" } {"total" : 7, "rid" : "41611381", "rname" : "Burger King, Popeye'S Chicken & Biscuits"} { "total" : 7, "rid" : "41572121", "rname" : "Luke'S Pizza" } { "total" : 7, "rid" : "41578481", "rname" : "Top Hot Bagels & Grill" } >