MongoDB Map Reduce

Source: Internet
Author: User
Tags emit

Map-reduce is a computational model, which simply means that a large amount of work (data) decomposition (MAP) is performed, and then the results are combined into the final result (Reduce).

Basic syntax for the MapReduce command:

Db.collection.mapReduce (

function () {emit (Key,value);},//map functions

function (key,values) {return reducefunction},//reduce functions

{

Out:collection,

Query:document,

Sort:document,

Limit:number

}

)

UseMapReduceto implement two functionsMapfunctions andReducefunction, Mapfunction Callemit (key, value),TraverseCollectionall the records in, will beKeywith thevaluepassed toReducefunction for processing.

The Map function must call emit (key, value) to return a key-value pair.

Parameter Description :

Map: Map function ( generate key-value pair sequence as the reduce function parameter ) .

Reduce: Statistical functions, The task of the reduce function is to turn key-values into key-value, which means The values array becomes a single value .

Out: The statistical result holds the collection ( without specifying the use of a temporary collection , which is automatically deleted after the client disconnects ).

Query: A filter condition in which only documents that meet the criteria call the map function. (Query,limit,sort can be combined arbitrarily)

Sort: sort parameter combined with limit (also in the Send to map To sort the document before the function), you can optimize the grouping mechanism.

Limit: The upper limit of the number of documents sent to the map function (if no limitisused, using sort alone is not very useful)

Examples of Use:

Db.posts.mapReduce (

function () {emit (this.user_name,1);},

function (key, values) {return array.sum (values)},

{

Query:{status: "Active"},

Out: "Post_total"

}

). Find ()

MongoDB 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.