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