Data logging and calculation results are returned during the aggregation operation. Aggregation Operations group values from multiple documents, and can perform various operations, grouping data to return a single result. The SQL COUNT (*) and group by are equivalent to the aggregation of MongoDB.
Aggregate () method
For aggregation in MongoDB, the aggregate () method should be used.
Grammar:
The basic syntax for the aggregate () method is as follows
>db. Collection_name. Aggregate(aggregate_operation)
Example:
In the collection, the following data is available:
{_id: ObjectId(7df78ad8902c)Title: ' MongoDB Overview ',Description: ' MongoDB is no SQL database ',By_user: ' Yiibai Point ',Url: ' Http://www.yiibai.com ',Tags: [' MongoDB ', ' Database ', ' NoSQL '],Likes: 100},{_id: ObjectId(7df78ad8902d)Title: ' NoSQL Overview ',Description: ' No SQL database is very fast ',By_user: ' Yiibai Point ',Url: ' Http://www.yiibai.com ',Tags: [' MongoDB ', ' Database ', ' NoSQL '],Likes: 10},{_id: ObjectId(7df78ad8902e)Title: ' Neo4j Overview ', Description: neo4j is no SQL database ' , By_user: , Url: , Tags: [ ' neo4j ' , ' database ' ,< Span class= "PLN" > ' NoSQL ' ], Likes: 750},
Now from the above collection, if you want to display a list that has a lot of user-written tutorials, then use the aggregate () method as follows:
>Db.MyCol.Aggregate([{$group: {_id: "$by _user",Num_tutorial: {$sum: 1}}}]){ "Result" : [ { "_ID" : "Yiibai Point", "num_tutorial" : 2< Span class= "PLN" > }, { : " Yiibai Point ",< Span class= "PLN" > "num_tutorial" : 1 } ], span class= "str" > "OK" : 1 }>
The above usage is equivalent to SQL query select By_user, COUNT (*) from MyCol GROUP by By_user
In the above example, we have grouped the fields by_user the document and incremented the sum of the previous values by_user each number of times. There is no clustered expression list.
An expression |
Description |
Example |
$sum |
Summarizes the values defined by all the files in the collection. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: "$likes"}}]) |
$avg |
The average calculated from all the given values in all document collections. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$avg: "$likes"}}]) |
$min |
Gets the minimum corresponding value in all files in the collection. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$min: "$likes"}}]) |
$max |
Gets the maximum of the corresponding values in all the files in the collection. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$max: "$likes"}}]) |
$push |
Values are inserted into an array generation document. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$push: "$url"}}]) |
$addToSet |
The value is inserted into an array of the resulting document, but no duplicates are created. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$addToSet: "$url"}}]) |
$first |
Gets the first document from the source document according to the grouping. Typically, this makes sense, along with some previous applications such as "$sort"-stage. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", First_url: {$first: "$url"}}]) |
$last |
Gets the final document from the source document according to the grouping. Usually, this makes sense, along with some previous applications such as "$sort"-stage. |
Db.mycol.aggregate ([{$group: {_id: "$by _user", Last_url: {$last: "$url"}}]) |
Piping Concepts
The UNIX command shell pipeline refers to the possibility of some inputs and outputs performing operations as input to the next command. The MongoDB aggregation framework also supports the same concept. There is a set of possible stages, each of which is a set of documents as input, and produces a result set of files (or eventually generated JSON documents at the end of the pipeline). And then again to be used for the next stage and so on.
A possible phase aggregation framework is as follows:
- $project: Used to select some specific fields from the collection.
- $match: This is a filtering operation, so you can reduce the amount of input for the given document as the next stage.
- $group: As discussed above, this is not an actual aggregation.
- $sort: Sort files.
- $skip: With this it is possible to skip forward a given number of documents in the list of files.
- $limit: This limits the number of documents to see the given number starting at the current position
- $unwind: This is used to close the position of the document using an array. When using an array, the data is a pre-joinded, and once again there are individual files, this operation will be canceled. Therefore, this stage, the number will increase the next stage of the document.
MongoDB Tutorial Lesson 11th MongoDB Aggregation