Part Two application chapter seventh MongoDB MapReduce

Source: Internet
Author: User
Tags emit install mongodb mongodb query

1. Introduction

MongoDB's mapreduce is equivalent to the group by in MySQL, so it's easy to use map/reduce on MongoDB, using MapReduce to implement two function map functions and the reduce function, the map function calls emit (Key,value), traversing all the records in the collection, passing key and value to the reduce function for processing, the map function and the reduce function can be implemented using JavaScript, A mapreduce operation can be performed by Db.runcommand or the MapReduce command:

Db.runcommand (

{mapreduce:<collection>,

Map:<mapfunction>,

Reduce:<reducefunction>

[, Query:<query filter object>]

[, sort:<sorts the input objects using this key,useful for optimization,like sorting by the emit key for fewer REDUCES&G t;]

[, Limit:<number of objects to return from collection>]

[, Out:<see output Options Below>]

[, Keeptemp:<true|false>]

[, Finalize:<finalizefunction>]

[, Scope:<object where fields go to JavaScript global scope>]

[, Verbose:true]

}

)

Parameter description:

  • MapReduce: The set of targets to manipulate.
  • Map: Map functions (Generate key-value pairs of sequences, as parameters of the reduce function).
  • Reduce: Statistical functions.
  • Query: Target record filtering.
  • Sort: Catalog record sorting.
  • Limit: Limits the number of target records.
  • Out: The statistical result holds the collection (without specifying the use of a temporary collection, which is automatically deleted after the client disconnects).
  • Keeptemp: Whether to keep the temporary collection.
  • Finalize: The final processing function (save the result set after the final collation of the reduce return result).
  • Scope: Import external variables to map, reduce, Finalze.
  • Verbose: Displays detailed time statistics.

2. Mapbelow we prepare some data:> Db.students.insert ({classid:1,name: ' Tom ', age:15});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:1,name: ' Jack ', age:12});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:2,name: ' Lily ', age:16});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:2,name: ' Tony ', age:9});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:2,name: ' Harry ', age:19});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:2,name: ' Vincent ', age:13});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:1,name: ' Bill ', age:15});
Writeresult ({"ninserted": 1})
> Db.students.insert ({classid:2,name: ' Bruce ', age:17});
Writeresult ({"ninserted": 1})
Next we show how to count the number of students in Class 1 and Class 2The map function must call emit (Key,value) to return a key-value pair, using this to access the currently pending document. > M=function () {Emit (this.classid,1)}
function () {Emit (this.classid,1)}
> value can be passed using Jsonobject (multiple property values are supported), such as:emit (This.classid,{count:1})
3. Reducethe reduce function receives a parameter similar to the group effect, combining the sequence of key values returned by the map into {key,[value1,value2,value3 ...]} Passed to reduce. > R=function (key,values) {var x=0;values.foreach (function (v) {x+=v}); return x;}
function (key,values) {var x=0;values.foreach (function (v) {x+=v}); return x;}
>
The reduce function counts these values and returns the results using Jsonobject.
4. Result> Res=db.runcommand ({mapreduce: "Students", Map:m,reduce:r,out: "Students_res"});
{
"Result": "Students_res",
"Timemillis": 1206,
"Counts": {
"Input": 8,
"Emit": 8,
"Reduce": 2,
"Output": 2
},
"OK": 1
}
View Statistical Results > Db.students_res.find ()
{"_id": 1, "Value": 3}
{"_id": 2, "Value": 5}
MapReduce () stores the results in the Students_res table.
4. FinalizeThe results of reduce () can be further processed using Finalize (). > f=function (key,value) {return{classid:key,count:value};}
function (Key,value) {return{classid:key,count:value};}
Let's calculate once again to see the results returned:> Res=db.runcommand ({mapreduce: "Students", Map:m,reduce:r,out: "Students_res",finalize:f});
{
"Result": "Students_res",
"Timemillis": 243,
"Counts": {
"Input": 8,
"Emit": 8,
"Reduce": 2,
"Output": 2
},
"OK": 1
}
> Db.students_res.find ();
{"_id": 1, "value": {"ClassID": 1, "Count": 3}}
{"_id": 2, "value": {"ClassID": 2, "Count": 5}}
Column names are changed with ClassID and count, and such lists are easier to understand.
5. OptionsWe can also add more control details. > Res=db.runcommand ({mapreduce: "Students", Map:m,reduce:r,out: "Students_res", Finalize:f,query:{age:{$lt: Ten}}});
{
"Result": "Students_res",
"Timemillis": 16,
"Counts": {
"Input": 1,
"Emit": 1,
"Reduce": 0,
"Output": 1
},
"OK": 1
}
> Db.students_res.find ();
{"_id": 2, "value": {"ClassID": 2, "Count": 1}}
You can see the first filter, only take age<10 data, and then the statistics, so there is no 1 classes of statistical data. -----------------------------------------MongoDB Article update----------------------------------------------------------

The first part of the basic chapter into MongoDB

The first part of the basic chapter II install MongoDB

Part I basic chapter III MONGODB architecture

The first part of the basic chapter fourth MongoDB Quick Start

The first part of the basic chapter Fourth MongoDB query

Part Two application chapter Fifth MongoDB advanced query

Part Two application chapter sixth MongoDB Gridfs







Part Two application chapter seventh MongoDB MapReduce

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.