MapReduce programming model usages in MongoDB _mongodb

Source: Internet
Author: User
Tags emit mongodb mongo shell

Note: The MongoDB used by the author is version 2.4.7.

Word Count Example:

To insert data for a word count:

Copy Code code as follows:

Db.data.insert ({sentence: ' Consider the following map-reduce operations on a collection orders that contains documents of T He following prototype '})
Db.data.insert ({sentence: ' I get the following error while I follow the code found in this link '})

The figure is concise and the data does not contain any punctuation marks. Write the following in the MONGO shell:

Copy Code code as follows:

var map = function () {
Split_result = This.sentence.split ("");
for (var i in Split_result) {
var word = split_result[i].replace (/(^\s*) | ( \s*$)/g, ""). toLowerCase (); Remove the possible spaces on both sides of the word and convert the word to lowercase
if (word.length!= 0) {
Emit (Word, 1);
}
}
}

var reduce = function (key, values) {
return array.sum (values);
}

Db.data.mapReduce (
Map
Reduce
{Out:{inline:1}}
)


The first and second parameters of Db.data.mapReduce specify that the input of map and Reduce,map is each document in the collection, the key value pair is generated by emit (), and reduce handles multiple values of the key.

The third parameter of MapReduce indicates that the MapReduce is carried out in memory and the result is returned, running as follows:

Copy Code code as follows:

{
"Results": [
{
"_id": "A",
"Value": 1
},
{
"_id": "Code",
"Value": 1
},
{
"_id": "Collection",
"Value": 1
},
{
"_id": "Consider",
"Value": 1
},
{
"_id": "contains",
"Value": 1
},
{
"_id": "Documents",
"Value": 1
},
{
"_id": "Error",
"Value": 1
},
{
"_id": "Follow",
"Value": 1
},
{
"_id": "Following",
"Value": 3
},
{
"_id": "Found",
"Value": 1
},
{
"_id": "Get",
"Value": 1
},
{
"_id": "I",
"Value": 2
},
{
"_id": "In",
"Value": 1
},
{
"_id": "Link",
"Value": 1
},
{
"_id": "Map-reduce",
"Value": 1
},
{
"_id": "of",
"Value": 1
},
{
"_id": "On",
"Value": 1
},
{
"_id": "Operations",
"Value": 1
},
{
"_id": "Orders",
"Value": 1
},
{
"_id": "Prototype",
"Value": 1
},
{
"_id": "That",
"Value": 1
},
{
"_id": "The",
"Value": 4
},
{
"_id": "This",
"Value": 1
},
{
"_id": "When",
"Value": 1
}
],
"Timemillis": 1,
"Counts": {
"Input": 2,
"Emit": 30,
"Reduce": 3,
"Output": 24
},
"OK": 1,
}


The value of the results is the processing result of MapReduce, Timemillis indicates the time spent; counts input indicates the number of documents entered, and emit indicates the number of calls to emit in the map. Reduce indicates the number of reduce (in this case, reduce is not required if a single number is 1), and output indicates the number of documents exported.

As you can see, the key _id is no longer automatically generated, but instead is replaced by the key in reduce. Of course, you can also enter the results into a new collection, for example:

Copy Code code as follows:
Db.data.mapReduce (map, reduce, {out: "Mr_result"})

You can then view the contents of the Mr_result collection:
Copy Code code as follows:
Db.mr_result.find ()

You can also use Db.runcommand to perform mapreduce tasks, which provide developers with more options, see information [1]. The information [2][3][4] provides more comprehensive content on mapreduce. data [5] The method of optimizing MapReduce task is given, and the data [6] is a Chinese translation of data [5].

It should be noted that the data [5] mentioned the use of Scopedthread () to create a thread, the author in the GUI tool Robomongo Shell to run the new Scopedthread () when the error: Referenceerror: Scopedthread is not defined (shell): 1

But it works in the MONGO Shell:

Copy Code code as follows:

> New Scopedthread ()
Sat 21:32:36.062 Error:need At least one argument at src/mongo/shell/utils.js:101

If you use a different programming language to manage MongoDB, you should use a thread that is built into the programming language when you want to use a thread.

On the mapreduce of MONGODB implementation, individuals feel that it is better to support multiple Mr Tasks to smooth the transition.

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.