SlideShare a Scribd company logo
SS18CO018
EXPERIMENT NO. 05
AIM: Implementation of aggregate operation on document
THEORY:
Aggregation operations process data records and return computed results.
Aggregation operations group values from multiple documents together,
and can perform a variety of operations on the grouped data to return a
single result.
• MongoDB provides three ways to perform aggregation
1)the aggregation pipeline,
2) the map-reduce function,
3)single purpose aggregation methods.
• Aggregation Pipeline
Aggregations operations process data records and return computed results.
Aggregation operations group values from multiple documents together, and can
perform a variety of operations on the grouped data to return a single result. In SQL
count(*) and with group by is an equivalent of MongoDB aggregation.
The aggregate() Method
For the aggregation in MongoDB, you should use aggregate() method.
Syntax
Basic syntax of aggregate() method is as follows –
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Following is a list of available aggregation expressions.
Expression Description
$sum Sums up the defined value from all
documents in the collection.
$avg Calculates the average of all given values
from all documents in the collection.
$min Gets the minimum of the corresponding
values from all documents in the
collection.
$max Gets the maximum of the corresponding
values from all documents in the
collection.
$push Inserts the value to an array in the
resulting document.
$addToSet Inserts the value to an array in the
resulting document but does not create
duplicates.
$first Gets the first document from the source
documents according to the grouping.
Typically this makes only sense together
with some previously applied
“$sort”-stage.
$last Gets the last document from the source
documents according to the grouping.
Typically this makes only sense together
with some previously applied
“$sort”-stage.
Following are the possible stages in aggregation framework −
$project − Used to select some specific fields from a collection.
$match − This is a filtering operation and thus this can reduce the amount of
documents that
are given as input to the next stage.
$group − This does the actual aggregation as discussed above.
$sort − Sorts the documents.
$skip − With this, it is possible to skip forward in the list of documents for a given
amount of documents.
$limit − This limits the amount of documents to look at, by the given number starting
from the current positions.
$unwind − This is used to unwind document that are using arrays. When using an
array, the data is kind of pre-joined and this operation will be undone with this to have
individual documents again. Thus with this stage we will increase the amount of
documents for the next stage.
For example:
In the example
Db.orders.aggregate(
[
{
$match: { status: “A” }
},
{
$group: { _id: “$cust_id”, total: { $sum: “$amount” } }
}
]
)
First Stage:
The $match stage filters the documents by the status field and passes to the next
stage those documents that have status equal to "A".
Second Stage:
The $group stage groups the documents by the cust_id field to calculate the sum
of the amount for each unique cust_id.The aggregation pipeline can use indexes to
improve its performance during some of its stages.
In addition, the aggregation pipeline has an internal optimization phase Aggregation
pipeline provides better performance and a more coherent interface than mapreduce.
,
2)Map – Reduce Function
MongoDB also provides map-reduce operations to perform aggregation. Map
reduce uses custom JavaScript functions to perform the map and reduce operations, as
well as the optional finalize operation.
Single Purpose Aggregation Operations
MongoDB Single Purpose Aggregation Operations
MongoDB also provides
1)db.collection.estimatedDocumentCount(),
2) db.collection.count()
3) db.collection.distinct().
All of these operations aggregate documents from a single collection. While these
operations provide simple access to common aggregation processes, they lack the
flexibility and capabilities of the aggregation pipeline and map-reduce.
CONCLUSION : Thus ,We have studied implementation of aggregate
operation on document.
OUTPUT
Create record of Employee for 10 people including field name, dept, post, year of
experience.
project fields name, dept & post
Update Experience of any one employee
Find out employees from computer department only
Find out the employees who are having experience above 10 years
Find out employees who are working in Civil department only.
Display seniority of employee from computer department.
From above collection show only 2 documents.

More Related Content

PPT
Counters for real-time statistics
DOCX
Different ways to load data in qlikview
PPTX
Sql Connection and data table and data set and sample program in C# ....
PPTX
Academy PRO: Elasticsearch Misc
PPTX
Apache Spark - Aram Mkrtchyan
PDF
Analytics with MongoDB Aggregation Framework and Hadoop Connector
PPTX
Hive query optimization infinity
PDF
MapReduce
Counters for real-time statistics
Different ways to load data in qlikview
Sql Connection and data table and data set and sample program in C# ....
Academy PRO: Elasticsearch Misc
Apache Spark - Aram Mkrtchyan
Analytics with MongoDB Aggregation Framework and Hadoop Connector
Hive query optimization infinity
MapReduce

What's hot (20)

PPT
Fosdem 2010 GT.M and OpenStreetMap
PPTX
CS 542 -- Query Optimization
PPT
Overview of query evaluation
PPTX
Advance MapReduce Concepts - Module 4
PDF
Lost In The Clouds
PDF
CMPE275-Project1Report
PPT
M/DB and M/DB:X
PPTX
JavaScript own objects(Web Technology)
PPTX
Dache: A Data Aware Caching for Big-Data using Map Reduce framework
PDF
2016 foss4 g track: developing and implementing spatial etl processes with...
PPTX
Database , 8 Query Optimization
PDF
Analyzing Air Quality Measurements in Macedonia with Apache Drill
PPTX
Entity framework
PPT
Server side geo_tools_in_drupal_pnw_2012
PPTX
Dache: A Data Aware Caching for Big-Data Applications Using the MapReduce Fra...
PPTX
Air Pollution in Nova Scotia: Analysis and Predictions
PPTX
Distributed Query Processing
PPT
Csc1100 lecture13 ch16_pt1
PPTX
Jiscad viz
PDF
Betting the Company on a Graph Database - Aseem Kishore @ GraphConnect Boston...
Fosdem 2010 GT.M and OpenStreetMap
CS 542 -- Query Optimization
Overview of query evaluation
Advance MapReduce Concepts - Module 4
Lost In The Clouds
CMPE275-Project1Report
M/DB and M/DB:X
JavaScript own objects(Web Technology)
Dache: A Data Aware Caching for Big-Data using Map Reduce framework
2016 foss4 g track: developing and implementing spatial etl processes with...
Database , 8 Query Optimization
Analyzing Air Quality Measurements in Macedonia with Apache Drill
Entity framework
Server side geo_tools_in_drupal_pnw_2012
Dache: A Data Aware Caching for Big-Data Applications Using the MapReduce Fra...
Air Pollution in Nova Scotia: Analysis and Predictions
Distributed Query Processing
Csc1100 lecture13 ch16_pt1
Jiscad viz
Betting the Company on a Graph Database - Aseem Kishore @ GraphConnect Boston...
Ad

Similar to Experiment no 05 (20)

ODP
Aggregation Framework in MongoDB Overview Part-1
PPTX
MongoDB Aggregation MongoSF May 2011
PPTX
Aggregation Presentation for databses (1).pptx
PPTX
MongoDB - Aggregation Pipeline
PPTX
Introduction to MongoDB
PPTX
Introduction To MongoDB
PPTX
Learning MongoDB Aggregations in 10 Minutes
PDF
MongoDB Aggregation Framework
PPTX
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
PPTX
Module3 for enginerring students ppt.pptx
PPTX
fard car.pptx
PDF
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
PPTX
MongoDB installation,CRUD operation & JavaScript shell
PPTX
Mongo Nosql CRUD Operations
PDF
full stack modul 5, mongodb,webpack,front-end,back-end
PDF
IRJET- Review of Existing Methods in K-Means Clustering Algorithm
PPTX
Introduction to MongoDB – A NoSQL Database
PPTX
MongoDB_ppt.pptx
PPTX
MongoDB-presentation.pptx
ODP
Scaling PostgreSQL With GridSQL
Aggregation Framework in MongoDB Overview Part-1
MongoDB Aggregation MongoSF May 2011
Aggregation Presentation for databses (1).pptx
MongoDB - Aggregation Pipeline
Introduction to MongoDB
Introduction To MongoDB
Learning MongoDB Aggregations in 10 Minutes
MongoDB Aggregation Framework
SH 2 - SES 3 - MongoDB Aggregation Framework.pptx
Module3 for enginerring students ppt.pptx
fard car.pptx
K Means Clustering Algorithm for Partitioning Data Sets Evaluated From Horizo...
MongoDB installation,CRUD operation & JavaScript shell
Mongo Nosql CRUD Operations
full stack modul 5, mongodb,webpack,front-end,back-end
IRJET- Review of Existing Methods in K-Means Clustering Algorithm
Introduction to MongoDB – A NoSQL Database
MongoDB_ppt.pptx
MongoDB-presentation.pptx
Scaling PostgreSQL With GridSQL
Ad

More from Ankit Dubey (20)

PDF
Unit 1 android and it's tools quiz {mad cwipedia}
PDF
Scheduling
PDF
Chapter 4
PDF
Chapter 3
PDF
Chapter 2
PDF
Chapter 1
PDF
Chapter 5
PDF
Ch5 cpu-scheduling
PDF
Ch4 threads
PDF
Ch3 processes
PPT
Ch2 system structure
PPT
Ch1 introduction-to-os
PDF
Android i
PDF
Mongodb mock test_ii
PDF
Android mock test_iii
PDF
Android mock test_ii
PDF
Ajp notes-chapter-06
PDF
Ajp notes-chapter-05
PDF
Ajp notes-chapter-04
PDF
Ajp notes-chapter-03
Unit 1 android and it's tools quiz {mad cwipedia}
Scheduling
Chapter 4
Chapter 3
Chapter 2
Chapter 1
Chapter 5
Ch5 cpu-scheduling
Ch4 threads
Ch3 processes
Ch2 system structure
Ch1 introduction-to-os
Android i
Mongodb mock test_ii
Android mock test_iii
Android mock test_ii
Ajp notes-chapter-06
Ajp notes-chapter-05
Ajp notes-chapter-04
Ajp notes-chapter-03

Recently uploaded (20)

PPTX
Artificial Intelligence
PPT
Occupational Health and Safety Management System
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
introduction to high performance computing
PPT
Total quality management ppt for engineering students
PPTX
Fundamentals of safety and accident prevention -final (1).pptx
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
PDF
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
PPT
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
UNIT - 3 Total quality Management .pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
Artificial Intelligence
Occupational Health and Safety Management System
Abrasive, erosive and cavitation wear.pdf
EXPLORING LEARNING ENGAGEMENT FACTORS INFLUENCING BEHAVIORAL, COGNITIVE, AND ...
Categorization of Factors Affecting Classification Algorithms Selection
introduction to high performance computing
Total quality management ppt for engineering students
Fundamentals of safety and accident prevention -final (1).pptx
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Information Storage and Retrieval Techniques Unit III
Integrating Fractal Dimension and Time Series Analysis for Optimized Hyperspe...
Analyzing Impact of Pakistan Economic Corridor on Import and Export in Pakist...
INTRODUCTION -Data Warehousing and Mining-M.Tech- VTU.ppt
Exploratory_Data_Analysis_Fundamentals.pdf
Current and future trends in Computer Vision.pptx
UNIT - 3 Total quality Management .pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Automation-in-Manufacturing-Chapter-Introduction.pdf

Experiment no 05

  • 1. SS18CO018 EXPERIMENT NO. 05 AIM: Implementation of aggregate operation on document THEORY: Aggregation operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. • MongoDB provides three ways to perform aggregation 1)the aggregation pipeline, 2) the map-reduce function, 3)single purpose aggregation methods. • Aggregation Pipeline Aggregations operations process data records and return computed results. Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result. In SQL count(*) and with group by is an equivalent of MongoDB aggregation. The aggregate() Method For the aggregation in MongoDB, you should use aggregate() method. Syntax Basic syntax of aggregate() method is as follows – >db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION) Following is a list of available aggregation expressions. Expression Description $sum Sums up the defined value from all documents in the collection. $avg Calculates the average of all given values from all documents in the collection. $min Gets the minimum of the corresponding values from all documents in the collection. $max Gets the maximum of the corresponding values from all documents in the collection. $push Inserts the value to an array in the resulting document. $addToSet Inserts the value to an array in the
  • 2. resulting document but does not create duplicates. $first Gets the first document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. $last Gets the last document from the source documents according to the grouping. Typically this makes only sense together with some previously applied “$sort”-stage. Following are the possible stages in aggregation framework − $project − Used to select some specific fields from a collection. $match − This is a filtering operation and thus this can reduce the amount of documents that are given as input to the next stage. $group − This does the actual aggregation as discussed above. $sort − Sorts the documents. $skip − With this, it is possible to skip forward in the list of documents for a given amount of documents. $limit − This limits the amount of documents to look at, by the given number starting from the current positions. $unwind − This is used to unwind document that are using arrays. When using an array, the data is kind of pre-joined and this operation will be undone with this to have individual documents again. Thus with this stage we will increase the amount of documents for the next stage. For example: In the example Db.orders.aggregate( [ { $match: { status: “A” } }, { $group: { _id: “$cust_id”, total: { $sum: “$amount” } } } ] ) First Stage: The $match stage filters the documents by the status field and passes to the next
  • 3. stage those documents that have status equal to "A". Second Stage: The $group stage groups the documents by the cust_id field to calculate the sum of the amount for each unique cust_id.The aggregation pipeline can use indexes to improve its performance during some of its stages. In addition, the aggregation pipeline has an internal optimization phase Aggregation pipeline provides better performance and a more coherent interface than mapreduce. , 2)Map – Reduce Function MongoDB also provides map-reduce operations to perform aggregation. Map reduce uses custom JavaScript functions to perform the map and reduce operations, as well as the optional finalize operation. Single Purpose Aggregation Operations MongoDB Single Purpose Aggregation Operations MongoDB also provides 1)db.collection.estimatedDocumentCount(), 2) db.collection.count() 3) db.collection.distinct(). All of these operations aggregate documents from a single collection. While these operations provide simple access to common aggregation processes, they lack the flexibility and capabilities of the aggregation pipeline and map-reduce. CONCLUSION : Thus ,We have studied implementation of aggregate operation on document.
  • 4. OUTPUT Create record of Employee for 10 people including field name, dept, post, year of experience.
  • 5. project fields name, dept & post Update Experience of any one employee Find out employees from computer department only Find out the employees who are having experience above 10 years
  • 6. Find out employees who are working in Civil department only. Display seniority of employee from computer department. From above collection show only 2 documents.