[MongoDB Learning Notes 30] aggregation Framework of MongoDB
Use the aggregation framework to transform and combine documents in a collection. Create a pipeline with multiple components to process a series of documents. These components include:
Filter (filtering)
Projection (projecting)
Grouping (grouping)
Sort (sorting)
Limit (limiting)
Skip (skipping)
For the actual collection framework in MongoDB, you need to pass these operations 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"}
You can see that the usage of find is similar.
(2) Group them according to the value of name field and calculate their number.
> db.post.aggregate ({$group: {"_ id": "$name", "count": {$sum:1}) {"_ id": null, "count": 13} {"_ id": "joe", "count": 2} >
(3) arrange the result set in descending order.
> 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) restrict the display of the first five 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 is similar to select in a relational row database in that it does not write to the collection and the result is only returned to the client.