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.

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 butdoes 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 documentsthat 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 ofEmployee 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 employeeswho are working in Civil department only. Display seniority of employee from computer department. From above collection show only 2 documents.