Aggregations operations procedure records facts and go back computed results. Aggregation operations institution values from more than one files collectively, and can carry out a diffusion of operations on the grouped information to return a single end result. In SQL depend(*) and with organization by way of is an equivalent of MongoDB aggregation.
The combination() Method
For the aggregation in MongoDB, you should use combination() technique.
Syntax
Basic syntax of mixture() approach is as follows −
>db.COLLECTION_NAME.aggregate(AGGREGATE_OPERATION)
Example
In the gathering you have the subsequent statistics −
{
_id: ObjectId(7df78ad8902c)
title: 'MongoDB Overview',
description: 'MongoDB is no sql database',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 100
},
{
_id: ObjectId(7df78ad8902d)
title: 'NoSQL Overview',
description: 'No sql database is very fast',
by_user: 'tutorials point',
url: 'http://www.tutorialspoint.com',
tags: ['mongodb', 'database', 'NoSQL'],
likes: 10
},
{
_id: ObjectId(7df78ad8902e)
title: 'Neo4j Overview',
description: 'Neo4j is no sql database',
by_user: 'Neo4j',
url: 'http://www.neo4j.com',
tags: ['neo4j', 'database', 'NoSQL'],
likes: 750
},
Now from the above collection, if you want to show a list stating how many tutorials are written by using every consumer, then you'll use the subsequent aggregate() technique −
> db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "tutorials point", "num_tutorial" : 2 }
{ "_id" : "Neo4j", "num_tutorial" : 1 }
>
Sql equal query for the above use case might be pick by_user, matter(*) from mycol organization by by_user.
In the above instance, we've grouped files by way of field by_user and on every incidence of via person preceding value of sum is incremented. Following is a list of to be had aggregation expressions.
Expression | Description | Example |
---|---|---|
$sum | Sums up the defined value from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}]) |
$avg | Calculates the average of all given values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}]) |
$min | Gets the minimum of the corresponding values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}]) |
$max | Gets the maximum of the corresponding values from all documents in the collection. | db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}]) |
$push | Inserts the value to an array in the resulting document. | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}]) |
$addToSet | Inserts the value to an array in the resulting document but does not create duplicates. | db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}]) |
$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. | db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}]) |
$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. | db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}]) |
Pipeline Concept
In UNIX command, shell pipeline approach the possibility to execute an operation on some input and use the output as the input for the following command and so on. MongoDB also supports same concept in aggregation framework. There is a hard and fast of feasible levels and each of these is taken as a set of documents as an enter and produces a ensuing set of documents (or the final resulting JSON report at the give up of the pipeline). This can then in flip be used for the next degree and so forth.
Following are the possible stages in aggregation framework −
- $task − Used to pick a few precise fields from a set.
- $suit − This is a filtering operation and consequently this will lessen the quantity of files that are given as enter to the following degree.
- $organization − This does the actual aggregation as mentioned above.
- $sort − Sorts the documents.
- $skip − With this, it is feasible to pass ahead within the listing of files for a given quantity of files.
- $limit − This limits the amount of documents to examine, through the given quantity starting from the current positions.
- $unwind − This is used to unwind document that are the use of arrays. When the usage of an array, the information is kind of pre-joined and this operation will be undone with this to have character files again. Thus with this level we will boom the quantity of files for the following level.