Aggregate equivalent to the group in MySQL and a series of operations
Official website address: http://docs.mongodb.org/manual/reference/sql-aggregation-comparison/
An expression |
Describe |
Instance |
$sum |
summarizes the values defined from all the files in the collection. |
db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$sum: "$likes"}}]) |
$avg |
|
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 |
db.mycol.aggregate ([{$group: {_id: "$by _user", num_tutorial: {$max: "$likes"}}]) |
$push |
|
db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$push: "$url"}}]) |
$addToSet |
|
db.mycol.aggregate ([{$group: {_id: "$by _user", url: {$addToSet: "$url"}}]) |
$first |
based on grouping the first document obtained from the source document. Typically, this makes sense, along with some of the previous applications "$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"}}]) |
The relevant operators are as follows:
$project: Used to select some specific fields from the collection. (alias, display or not display)
$match: This is a filtering operation, so you can reduce the amount of input for the given document as the next stage. (query criteria)
$group: As discussed above, this is not an actual aggregation. (fields that need to be grouped, sum, etc. are also carried out here)
$sort: Sort files. (sort)
$skip: With this it is possible to skip forward a given number of documents in the list of files. (for pagination)
$limit: This limits the number of documents to see the given number starting at the current position (for pagination)
$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. (this is seldom used)
The actions in Java correspond to the following:
Basicdbobject fields = new basicdbobject ("email", 1); dbobject project = new basicdbobject ("$project", fields ); basicdbobject groupfilters = new basicdbobject ("_id", "$name"); groupfilters.put ("Sumage", new basicdbobject ("$sum", "$age")); groupfilters.put ("Totalage", new basicdbobject ("$avg", "$age")); basicdbobject group = new basicdbobject ("$group" , groupfilters); aggregationoutput aggrresult = this.mongotemplate.getcollection ("Test"). Aggregate ( Matchopt,project, group); //If there are sort,skip, then continue to append //or you can add all the operate to the list, as follows list list = new arraylist (); list.add (group); list.add (project ); aggregationoutput Aggrresult = this.mongotemplate.getcollection ("Test"). Aggregate (list); // So the same can
This article is from the "Bulajunjun" blog, make sure to keep this source http://5148737.blog.51cto.com/5138737/1638101
MongoDB Aggregate Aggregation