Java Login

About javalogin.com

Hello guys,
javalogin.com is for Java and J2EE developers, all examples are simple and easy to understand 

It is developed and maintained by Vaibhav Sharma. The views expressed on this website are his own and do not necessarily reflect the views of his former, current or future employers. I am professional Web development. I work for an IT company as Senior Consultant. Primary I write about spring, hibernate and web-services. I am trying to present here new technologies.


     << Previous
Next >>     


MongoDB Aggregation


Operations that process data sets and return calculated results are called aggregations. Aggregation in MongoDB is nothing but an operation used to process the data that returns the computed results. Aggregation within any database is extremely important. Users of SQL will understand this. Imagine if you have one table of users and one table of clicks. Let's say you wish to aggregate the clicks per user and groups on certain pages where those clicks occurred. Aggregation basically groups the data from multiple documents and operates in many ways on those grouped data in order to return one combined result. In sql count(*) and with group by is an equivalent of MongoDB aggregation.

MongoDB provides a number of aggregation tools that go beyond basic query functionality. These range from simply counting the number of documents in a collection to using MapReduce to do complex data analysis. In order to perform the aggregate function in MongoDB, aggregate () is the function to be used. Following is the syntax for aggregation :

db.collection_name.aggregate(aggregate_operation)

Data in JSON format, shows the employee details.


{ "_id" : "52b213b38594d8a2be17c780" , "employeeName" : "Mark", "employeeJoingingDate" : "2015-11-12T00:00:00Z", "salary" : "25000" }

{ "_id" : "52b213b38594d8a2be17c781" , "employeeName" : "Steev", "employeeJoingingDate" : "2015-11-04T00:00:00Z", "salary" : "20000" }

{ "_id" : "52b213b38594d8a2be17c782" , "employeeName" : "Daewon", "employeeJoingingDate" : "2012-11-01T00:00:00Z", "salary" : "25000" }

{ "_id" : "52b213b38594d8a2be17c783" , "employeeName" : "Gabriel", "employeeJoingingDate" : "2012-10-31T00:00:00Z", "salary" : "25000" }

{ "_id" : "52b213b38594d8a2be17c784" , "employeeName" : "Pacey", "employeeJoingingDate" : "2015-10-31T00:00:00Z", "salary" : "25000" }

{ "_id" : "52b213b38594d8a2be17c785" , "employeeName" : "Babette", "employeeJoingingDate" : "2017-01-31T00:00:00Z", "salary" : "20000" }

{ "_id" : "52b213b38594d8a2be17c786" , "employeeName" : "Addy", "employeeJoingingDate" : "2016-10-29T00:00:00Z", "salary" : "18000" }

{ "_id" : "52b213b38594d8a2be17c787" , "employeeName" : "Samson", "employeeJoingingDate" : "2011-10-29T00:00:00Z", "salary" : "20000" }

{ "_id" : "52b213b38594d8a2be17c788" , "employeeName" : "Oberon", "employeeJoingingDate" : "2013-10-29T00:00:00Z", "salary" : "20000" }

{ "_id" : "52b213b38594d8a2be17c789" , "employeeName" : "Yadira", "employeeJoingingDate" : "2012-10-29T00:00:00Z", "salary" : "18000" }



$match Operator
The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage. The following command selects the employee where the employeeName field equals to Babette.


> db.empdetails.aggregate(
[ { $match : { employeeName : "Babette" } } ]
);

{ "_id" : "52b213b38594d8a2be17c785" , "employeeName" : "Babette", "employeeJoingingDate" : "2017-01-31T00:00:00Z", "salary" : "20000" }

> db.empdetails.aggregate(
[{$match:{$or:[
{employeeName:"Babette"},
{employeeName: "Yadira"}
]}}]);

{ "_id" : "52b213b38594d8a2be17c785" , "employeeName" : "Babette", "employeeJoingingDate" : "2017-01-31T00:00:00Z", "salary" : "20000" }
{ "_id" : "52b213b38594d8a2be17c789" , "employeeName" : "Yadira", "employeeJoingingDate" : "2012-10-29T00:00:00Z", "salary" : "18000" }


$group Operator
group allows you to perform more complex aggregation. You choose a key to group by, and MongoDB divides the collection into separate groups for each value of the chosen key. For each group, you can create a result document by aggregating the documents that are members of that group. MongoDB supports quite a few group operators including $max, $min, $avg and $sum. If you are familar with SQL, you will instantly recognize those operators from your own queries.


> db.employees.aggregate(
[
{
$group : {_id : "$salary",
Employees: { $sum: 1 }
}
}
]
)

{"_id" : "25000", "Employees" : 4}
{"_id" : "20000", "Employees" : 4}
{"_id" : "18000", "Employees" : 2}


distinct keyword
With distinct we obtain the unique values of a specific field, for example:


> db.employees.distinct("salary") ["25000", "20000", "18000" ]

$sort Operator
Sorts all input documents and returns them to the pipeline in sorted order.
The $sort stage has the following prototype form:
{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }
$sort operator use 1 or -1 indicates the order (1 for ascending and -1 for descending)
Example --


db.employees.find().sort({"employeeName":1})

{ "_id" : "52b213b38594d8a2be17c786", "employeeName" : "Addy", "employeeJoingingDate" : "2016-10-29T00:00:00Z", "salary" : "18000"}

{ "_id" : "52b213b38594d8a2be17c785", "employeeName" : "Babette", "employeeJoingingDate" : "2017-01-31T00:00:00Z", "salary" : "20000"}

{ "_id" : "52b213b38594d8a2be17c782", "employeeName" : "Daewon", "employeeJoingingDate" : "2012-11-01T00:00:00Z", "salary" : "25000"}

{ "_id" : "52b213b38594d8a2be17c783", "employeeName" : "Gabriel", "employeeJoingingDate" : "2012-10-31T00:00:00Z", "salary" : "25000"}

{ "_id" : "52b213b38594d8a2be17c780", "employeeName" : "Mark", "employeeJoingingDate" : "2015-11-12T00:00:00Z", "salary" : "25000"}

.............
.............

Now we are using sort operator with aggregate function ---


> db.employees.aggregate( [ { $project : { _id: 0, employeeName : 1 , salary : 1 } } ,
{ $sort : { employeeName : 1 } }
] );
{"employeeName" : "Addy", "salary" : "18000"}
{"employeeName" : "Babette", "salary" : "20000"}
{"employeeName" : "Daewon", "salary" : "25000"}
{"employeeName" : "Gabriel", "salary" : "25000"}
{"employeeName" : "Mark", "salary" : "25000"}
.............
.............


aggregate using project, match and sort operator----


> db.employees.aggregate( [ { $project : { _id: 0, employeeName : 1 , salary : 1 } } ,
{ $match : { salary : "20000" } },
{ $sort : { employeeName : 1 } }
] );

{ "employeeName" : "Babette", "salary" : "20000"}
{ "employeeName" : "Oberon", "salary" : "20000"}
{ "employeeName" : "Samson", "salary" : "20000"}
{ "employeeName" : "Steev", "salary" : "20000"}


$count Operator
As you can suppose this operation returns us the number of documents that match a given condition.


> db.employees.count()
10

db.employees.count( { "salary" : { $eq : "20000" } } )
4


$unwind operator
The MongoDB $unwind stages operator is used to re-constructing an array field from the input documents to output a document for each element. Every output document is the input document with the value of the array field replaced by the element.


> db.collectionName.aggregate( [ { $unwind : "$fieldName" } ] );


$project Operator This operation let us select the fields we want to obtain. The $project function in MongoDB passes along the documents with only the specified fields to the next stage in the pipeline. This may be the existing fields from the input documents or newly computed fields.

Syntex:
{ $project: { <specifications> } }
The specification for $project command contain the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting the values of existing fields. 0 and 1 value use with field for exclusion and inclusion.


> db.employees.aggregate( [ { $project : { _id: 0, employeeName : 1 , salary : 1 } } ] );

{ "employeeName" : "Mark", "salary" : "25000"}
{ "employeeName" : "Steev", "salary" : "20000"}
{ "employeeName" : "Daewon", "salary" : "25000"}
{ "employeeName" : "Gabriel", "salary" : "25000"}
{ "employeeName" : "Pacey", "salary" : "25000"}
{ "employeeName" : "Babette", "salary" : "20000"}
{ "employeeName" : "Addy", "salary" : "18000"}
{ "employeeName" : "Samson", "salary" : "20000"}
{ "employeeName" : "Oberon", "salary" : "20000"}
{ "employeeName" : "Yadira", "salary" : "18000"}

Now here are some operators used by aggregate function -
$first - Returns the first document from the document
$last - Returns the last document from the document
$sum - Summ the defined values from all the documents in a collection
$push - Inserts values to an array in the resulting document
$max - Return the maximum of all values of documents in a collection
$min - Return the minimum of all values of documents in a collection
$avg - Calculates the average values from all the documents in a collection
$addToSet - Inserts values to an array but no duplicates in the resulting document


     << Previous
Next >>