Preface
There are 3 days to end in 2014, and this month's plan has been completed in addition to the summary of MongoDB's other plans. Recently, lazy research on MongoDB has been suspended for a long time. In the last three days, the study of MongoDB is done to summarize and comb;
First, the basic concept
for the aggregation framework, I am in the official documentation ( http://docs.mongodb.org/manual/core/aggregation-introduction/ ) to find an introduction to it. The translation probably means the following:
Aggregations are actions that process data records and return the results of a calculation. MongoDB provides a powerful set of aggregation operations for checking and calculating data sets. Running data aggregation in a Mongod instance simplifies application code and constrains resource requirements; As with queries, aggregation operations in MongoDB take a collection of documents as input parameters, returning the results as one or more documents.
Using the aggregation framework in MongoDB, you can transform and combine the documents in the collection. Basically create a pipeline (pipeline) with multiple builds for processing a series of documents. These constructs include filtering (filtering), casting (projecting), grouping (grouping), sorting (sorting), throttling (limiting), and skipping (skipping).
The following is an example diagram in the official documentation:
Second, the basic operator
For example, there is a collection of magazine articles saved. You may want to find out which author of the article is published, assuming that each article is saved as a document in MongoDB, you can create the pipeline as follows.
1. Project the author of each article document
2. Sort the author by name, counting the number of occurrences of each name
3. Descending the number of times the author appears by name
4. Limit the return result to the first five
For the action symbol instance:
1. {' $project ': {' auther ': 1}} to project ' author ' from each document; {' id ': ID, ' author ': author}
2. {' $group ': {' Id: ' $author ', ' count ': {' $sum ': 1}} So the author will be sorted by name, and each author's name will be added to the count of each writer once each occurrence.
After you perform this step, each document in the result collection is such a structure: {"_id": "AuthorName", "Count": Articlecount}
3. {"$sort": {"Count":-1}} This action will sort the documents in the result collection according to the "Count" field in descending order
4. {"$limit": 5} The result is displayed as the top five documents for the current result.
Other operating symbols: Http://docs.mongodb.org/manual/reference/operator/aggregation-pipeline
"MongoDB" MongoDB's aggregation framework (i)