Mongodb:map-reduce

Source: Internet
Author: User

Map-reduce is a data processing program (paradigm) that considers large data to get useful aggregation results. For map-reduce operations, MongoDB provides the MapReduce command.

Consider the following map-reduce operation:


In this map-reduce operation, MongoDB applies a map operation to each input document (for example, a document in the collection that satisfies the query criteria). The map function enters a key-value pair. For those keys that have multiple values, MongoDB uses the reduce phase, which collects and compresses the aggregated data. Mongo-db then the result exists in a set. The output of the reduce function can optionally be passed to a finalize function to further compress or process the aggregated result.

In MongoDB, all the Map-reduce functions are JavaScript code, all running in the Mongod process. The Map-reduce operation accepts a collection of documents as input and can perform arbitrary sorting and throttling before the map stage. MapReduce can return the result of a map-reduce operation as a document, or it may write a result to the collection. The collection of inputs and outputs may be shared.

Note:

For most aggregation operations, the aggregation pipeline provides better performance and a more consistent interface. However, the Map-reduce operation provides the flexibility that is not available in the aggregation pipeline.


Map-reduce JavaScript functions

The mongodb,map-reduce operation uses a custom function to go to map, or to associate a key with a value. If a key has more than one value and it corresponds, the reduce operation "operation" the value of the key to a single object (the reduces of the values for the key to a).

The custom JavaScript function gives map-reduce flexibility. For example, when working with a document, the map function produces more than one key-value pair to match or no key-value pair to match. The Map-reduce function can also use a custom JavaScript function to make final changes to the results at the end of the map and reduce function operations.


Map-reduce behavior

The Mongodb,map-reduce function is able to write results or return results to the collection online. If you write the output of Map-reduce to a collection, you can perform subsequent map-reduce operations on the same input collection, merging overrides, merging, or cutting the previous results.

When the results of the map-reduce operation are returned online, the resulting document must be within the Bson document size limit, which is currently 16M.

MongoDB supports map-reduce operations on shared collections and can also output results to shared collections.


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.