[DOCS-1248] Comment on: "manual/applications/map-reduce.txt" Created: 16/Mar/13  Updated: 03/Nov/17  Resolved: 02/Apr/13

Status: Closed
Project: Documentation
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: auto Assignee: Kay Kim (Inactive)
Resolution: Done Votes: 0
Labels: collector-298ba4e7
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

redhat linux 64bit
mongodb-linux-i686-2.2.3

Location: http://docs.mongodb.org/manual/applications/map-reduce/
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11
Referrer: http://www.cnblogs.com/daizhj/archive/2010/06/10/1755761.html
Screen Resolution: 1366 x 768
repo: docs
source: applications/map-reduce


Participants:
Days since reply: 10 years, 46 weeks, 1 day ago

 Description   

I find a strange problem when I use map-reduce,here is the description:
first I insert data into database like this:
//insert.js
for(var i=0;i<5;i++){
db.RegistRecord.save(

{"ip":"11.11.11.11","account":"ongyong"}

);
}

for(var i=0;i<2;i++){
db.RegistRecord.save(

{"ip":"11.11.11.22","account":"ongyong22"}

);
}
for(var i=0;i<1;i++){
db.RegistRecord.save(

{"ip":"11.11.11.33","account":"ongyong33"}

);
}

then run map-reduce through a javascript like this:

//regmp.js
printjson("job start");
var map = function() {
emit(this.ip,

{value: 1}

);
}

var reduce = function(key, values) {
var count = 0;
values.forEach(function(v)

{ count += v['value']; }

);
return

{count: count }

;

}

var res = db.runCommand(

{mapreduce:"RegistRecord",map:map, reduce:reduce, out:"log_results"}

);
printjson("job end")

Ok,you'll find the result is no problem and like this:
/* 0 */
{
"_id" : "11.11.11.11",
"value" :

{ "count" : 5.0 }

}

/* 1 */
{
"_id" : "11.11.11.22",
"value" :

{ "count" : 2.0 }

}

/* 2 */
{
"_id" : "11.11.11.33",
"value" :

{ "value" : 1.0 }

}

but,when the records increase to 2000,strange problem comes:
insert data first like this :
//insert.js
for(var i=0;i<2000;i++){
db.RegistRecord.save(

{"ip":"11.11.11.11","account":"ongyong"}

);
}

for(var i=0;i<2;i++){
db.RegistRecord.save(

{"ip":"11.11.11.22","account":"ongyong22"}

);
}
for(var i=0;i<1;i++){
db.RegistRecord.save(

{"ip":"11.11.11.33","account":"ongyong33"}

);
}

run the same script and the result is like this :
/* 0 */
{
"_id" : "11.11.11.11",
"value" :

{ "count" : NaN }

}

/* 1 */
{
"_id" : "11.11.11.22",
"value" :

{ "count" : 2.0 }

}

/* 2 */
{
"_id" : "11.11.11.33",
"value" :

{ "value" : 1.0 }

}

see the fird record ? "NaN"??
And I also find that the data reduce function received is strange:

/* 0 */
{
"_id" : "11.11.11.22",
"value" : {
"count" : [

{ "value" : 1.0 }

,

{ "value" : 1.0 }

]
}
}

/* 1 */
{
"_id" : "11.11.11.33",
"value" :

{ "value" : 1.0 }

}

/* 2 */
{
"_id" : "11.11.11.11",
"value" : {
"count" : [{
"count" : [

{ "value" : 1.0 }

,

{ "value" : 1.0 }

,

{ "value" : 1.0 }

,
。。。。

Sorry for my poor English...
hope for your response!



 Comments   
Comment by Kay Kim (Inactive) [ 02/Apr/13 ]

Thank you so much for your comment.

With map-reduce, the reduce function must have the following property:

reduce( key, [ reduce(key, valuesArray) ] ) == reduce( key, valuesArray )

Your reduce function does not share the property:

var reduce = function(key, values) {
var count = 0;
values.forEach(function(v)

{ count += v['value']; }

);
return

{count: count }

;
}

that is

reduce("11.11.11.11", [ reduce("11.11.11.11", [

{ value: 1 }

] ) ] ) does not equal reduce( "11.11.11.11", [

{value: 1}

] )

If you try:

var map = function() {
emit(this.ip, 1 );
}

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

this should work for you.

The http://docs.mongodb.org/manual/reference/method/db.collection.mapReduce page lists (around the middle of the page) the requirements for the map and reduce functions

Hope this answers your question.

Generated at Thu Feb 08 07:40:35 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.