简体   繁体   中英

when I use the map-reduce of mongodb, why does it nest?

I insert into mongo with the code:

for (var i=0; i<1000; i++) {
db.test.insert({ "name" : "user"+i, "age":i%17 , "created_at" : new Date() });
} 

and the map function is

var m = function(){ emit(this.age,this.name); }

the reduce function is

var r=function(key,values){ var ret={age:key,names:values}; return ret;}

But, when I run the code

db.runCommand({mapreduce:"test",map:m,reduce:r,     out:"t_age_names"} )

I get 17 json and everyone is like

{ "_id" : 8, "value" : { "age" : 8, "names" : [ { "age" : 8, "names" : [ { "age" : 8, "names" : [ "user8", "user25", "user42", "user59", "user76", "user93", "user110", "user127", "user144", "user161", "user178", "user195" ] }, "user212", "user229", "user246", "user263", "user280", "user297", "user314", "user331", "user348", "user365", "user382", "user399" ] }, { "age" : 8, "names" : [ { "age" : 8, "names" : [ "user416", "user433", "user450", "user467", "user484", "user501", "user518", "user535", "user552", "user569", "user586" ] }, "user603", "user620", "user637", "user654", "user671", "user688", "user705", "user722", "user739", "user756", "user773", "user790" ] }, { "age" : 8, "names" : [ "user807", "user824", "user841", "user858", "user875", "user892", "user909", "user926", "user943", "user960", "user977", "user994" ] } ] } }

Why does it nest?

Why does it nest?
And the length of every array is 12, it seems like to be caused by emit function, is it?

Your reduce violates [at least] one of the requirements :

the type of the return object must be identical to the type of the value emitted by the map function.

So either change what reduce outputs, or what map does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM