MongoDB Aggregation Operations

Source: Internet
Author: User
Tags mongo shell

Aggregation Introduction

The aggregation operation is the operation that comes out of the data record and returns the computed result. MongoDB provides a rich aggregation operation to detect and execute calculations on datasets. Data aggregation running on Mongod simplifies code and resource constraints.

As with queries, the aggregation operation in MONGO uses collections as input and returns one or more document as the output.

Aggregation mode

    • Aggregation pipeline

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

The most basic pipeline stage provides a filter (Filters) to act like a query, and document conversion (Doc transformations) to modify the form of the output document.

Other pipelines provide tools for grouping and sorting specific fields, as well as for aggregating array contents. Also, the pipeline stage can use operators for tasks, such as averaging or connecting a string.

The pipeline uses MONGODB native operations to provide efficient data aggregation and is an ideal data aggregation method within MongoDB.


The aggregated Operation pipeline operation with annotations above has two stages: $match and $group


    • Map-reduce

MongoDB also provides map-reduce operations to perform aggregation operations. In general, the map-reduce operation has two phases: the map stage to process each document and output one or more documents for each input document, and the reduce phase consolidates the output of the map operation. The map-reduce has an optional finalize phase to make the final changes to the results. As with other aggregation operations, map-reduce points to the query criteria to select the input document and sort and limit the results.

Map-reduce uses the custom javasript function to perform map and reduce operations, as well as optional finalize operations. Although a custom javasript relative to the aggregation pipeline provides greater flexibility, the general map-reduce is relatively inefficient and complex.

Also, the output set of the map-reduce operation is 16M larger than the output limit of the set pipe.

Note:

Starting with MongoDB2.4, specific MONGO shell functions and properties cannot be used by map-reduce operations. MongoDB2.4 also provides support for performing multiple JavaScript operations at the same time. Before MongoDB2.4, JavaScript code can only be executed in single-threaded execution, which poses difficulties for map-reduce parallelism.




    • Single-Purpose aggregation operations

For many of the general single-purpose aggregation operations (singleton Purpose aggregation operation), MONGODB provides special-purpose database commands. These general aggregation operations are: Returns the number of matching documents, returns a unique value for a field, and a grouping based on a field value. All of these operations are from a single collection. While these operations provide a simple way to handle common aggregation operations, they lack flexibility and the ability to do things like converged 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.