Use the aggregation framework to transform and combine documents in a collection. Create a pipeline (pipeline) with multiple artifacts for processing a series of documents. These artifacts include:
Filter (filtering)
Projection (projecting)
Grouping (grouping)
Sort (sorting)
Restrictions (limiting)
Skip (skipping)
The actual collection framework in MongoDB requires that these operations be passed to the aggregate function, for example:
(1) Project the Name field of the document
> db.post.aggregate ({$project: {"name": 1}}) { "_id" : objectid (" 54a530c3ff0df3732bac1681 "), " name " : " Joe " } { " _id " : objectid ("54a530c3ff0df3732bac1680"), "name" : "Joe" } { "_id" : objectid ("54a9700e1b5afd45354fd086") } { "_id"  : objectid ("54a9701c1b5afd45354fd087") } { "_id" : objectid (" 54a970281b5afd45354fd088 ") } { " _id " : objectid (" 54a970351b5afd45354fd089 ") } { " _id " : objectid (" 54a970781b5afd45354fd08a ") } { " _id " : objectid (" 54a970831b5afd45354fd08b ") } { " _id " : objectid (" 54a970901b5afd45354fd08c ") } { " _id " : objectid (" 54a9709c1b5afd45354fd08d ") } { " _id " : objectid (" 54aa8a90652d8bdfa0566d34 " ) } { "_id" : objectid ("54aa97b894dcf31069b590ca") } { "_id" : objectid ("54AA97D794DCF31069B590CB") } { "_id" : objectid ("54aa97f294dcf31069b590cc") } { "_id" : objectid ("54aff7f43bd1048e7b585e39") } > db.post.aggregate ({$ project:{"name": 1, "_id": 0}) { "name" : "Joe" } { "name" : "Joe"  }
It can be seen that the use of find is similar;
(2) Grouping and calculating the number of values in the Name field
> db.post.aggregate ({$group: {"_id": "$name", "Count": {$sum: 1}}}) {"_id": null, "Count": +} {"_id": "Joe", "C Ount ": 2} >
(3) descending order of result set
> db.post.aggregate ({$sort: {"id": -1}}) { "_id" : objectid (" 54AA97D794DCF31069B590CB "), " id " : 13, " fruit " : [ " apple ", " Kumquat ", "Orange", "fruit01" ] } { "_id" : objectid (" 54aa97b894dcf31069b590ca "), " id " : 12, " fruit " : [ " apple ", " banana ", "Peach" ] } { "_id" : objectid ("54aa8a90652d8bdfa0566d34 "), " id " : 11, " test10 " : 11 } { " _id " : objectid ("54a9709c1b5afd45354fd08d"), "id" : 10, "test10"  : 10 } { "_id" : objectid ("54a970901b5afd45354fd08c"), "id" : 9 , "Test9" : 9 } { "_id" : objectid (" 54a970831b5afd45354fd08b "), " id " : 8, "Test8" : 8 } { "_id" : objectid (" 54a970781b5afd45354fd08a "), " id " : 7, " test7 " : 7 } { "_id" : objectid ("54a970351b5afd45354fd089"), "id" : 6, "test6"  : 6 } { "_id" : objectid ("54a970281b5afd45354fd088"), "id" : 5, "Test5" : 5 } { "_id" : objectid (" 54a9701c1b5afd45354fd087 "), " id " : 4, " test4 " : 4 } { "_id" : objectid ("54a9700e1b5afd45354fd086"), "id" : 3, "test3"  : 3 } { "_id" : objectid ("54a530c3ff0df3732bac1681"), "id" : 2, "name" : "Joe", "age" : 30, "Sex" : 1, " School " : " Marry " } { "_id" : objectid ("54a530c3ff0df3732bac1680"), "id" : 1, "name" : "Joe", "age" : 30, "comments" : [ "Test2", " Test9 ", " test5 ], "Sex" : 1, "school" : "Marry" } { "_id" : objectid ("54aa97f294dcf31069b590cc"), "Push" : { " Fruit " : " fruit01 " } } { " _id " : objectid (" 54aff7f43bd1048e7b585e39 "), " username " : " Sid ", " loc " : { " IP " : "1.2.3.4", "City" : "Springfield", "state" : "NY" } } >
(4) Limit display of the first 5 results after sorting
> db.post.aggregate ({$sort: {"id": -1}},{$limit: 5}) { "_id" : objectid ("54AA97D794DCF31069B590CB"), "id" : 13, "fruit" : [ "Apple", "Kumquat", "Orange", "fruit01" ] } { "_id" : objectid ("54aa97b894dcf31069b590ca"), "id" : 12, "fruit" : [ "Apple", "banana", "peach" ] } { "_id" : objectid ("54aa8a90652d8bdfa0566d34"), "id" : 11, "test10" : 11 } { "_id" : objectid ("54a9709c1b5afd45354fd08d"), "id"  : 10, "test10" : 10 } { "_id" : objectid (" 54a970901b5afd45354fd08c "), " id " : 9, " test9 " : 9 }
The aggregation framework resembles a select in a relational row database, does not write to the collection, and the results are returned only to the client.
This article is from the "Margin with Wish" blog, please be sure to keep this source http://281816327.blog.51cto.com/907015/1604102
"MongoDB Learning Note 30" MongoDB's aggregation framework