MongoDB aggregation operations,

Source: Internet
Author: User
Tags mongo shell

MongoDB aggregation operations,

Introduction to aggregation

An aggregation operation is an operation that records data and returns computation results. MongoDB provides rich aggregation operations to detect and execute computation on datasets. Data aggregation running on mongod simplifies code and resource restrictions.

Like a query, the aggregation operation in Mongo uses collections as the input and returns one or more documents as the output.

Aggregation Mode

  • Aggregate MPs queue

MongoDB2.2 introduces a new aggregation framework: Aggregation pipeline, which is a model based on the concept of data processing pipeline. The document inputs a multi-stage pipeline and converts the document into an aggregated result.

The most basic pipeline stage providesFilter(Filters) to operate like a query, andDocument Conversion(Document transformations) to modify the form of the output Document.

Other Pipelines provide tools for grouping and sorting specific fields, as well as tools for aggregating array content. In addition, the MPs queue stage can use operators for tasks, such as calculating the average value or connecting a string.

Pipelines use MongoDB native operations to provide effective data aggregation, which is also an ideal data aggregation method in MongoDB.


The preceding aggregated Operation pipeline has two phases: $ match and $ group.


  • Map-Reduce

MongoDB also provides map-reduce operations to perform aggregation operations. Generally, the map-reduce operation has two phases: the Map stage processes each document and outputs one or more documents for each input document. The Reduce stage integrates the output of the map operation. Map-reduce has an optional finalize stage to make final modifications to the results. Like other aggregate operations, map-reduce specifies the query conditions for selecting input documents and sorting and limiting results.

Map-reduce uses the custom rript function to perform map and reduce operations and optional finalize operations. Although the custom aggregate ript provides greater flexibility than the aggregate pipeline, map-reduce is generally relatively inefficient and complex.

In addition, the output set of the map-reduce operation is 16 MB larger than the output limit of the collection pipeline.

Note:

From MongoDB2.4, specific mongo shell functions and attributes cannot be used by map-reduce operations. MongoDB2.4 also supports executing multiple Javascript operations at the same time. Before MongoDB2.4, javascript code can only be executed in a single thread, which makes it difficult to parallel map-reduce.




  • Single-purpose aggregation operations

For many generalSingle-purpose aggregation operations(Single purpose aggregation operation), MongoDB provides database commands for special purposes. These general aggregation operations are: return the number of matched documents, return the unique value of a field, and group based on a field value. All these operations come from a collection. Although these operations provide simple general aggregate operation processing methods, they lack flexibility and capabilities like aggregation pipelines and Map-reduce.







Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.