MongoDB MapReduce, second argument of reduce function is multidimensional array -
i tried utilize mapreduce collection. debug returned vals value passed sec argument reduce function, this:
db.runcommand({ "mapreduce":"mycollection", "map":function() { emit( { country_code:this.cc, partner:this.di, registeredperiod:math.floor((this.ca - 1399240800)/604800) }, { count:math.ceil((this.lla - this.ca)/86400) }); }, "reduce":function(k, vals) { homecoming { 'count':vals }; }, "query":{ "ca":{ "$gte":1399240800 }, "di":405, "cc":"1" }, "out":{ "inline":true } }); and got result this:
{ "results" : [ { "_id" : { "country_code" : "1", "distribution" : 405, "installationperiod" : 0 }, "value" : { "count" : [ { "count" : 37 }, { "count" : 38 } ] } }, { "_id" : { "country_code" : "1", "distribution" : 405, "installationperiod" : 1 }, "value" : { "count" : 36 } }, { "_id" : { "country_code" : "1", "distribution" : 405, "installationperiod" : 4 }, "value" : { "count" : [ { "count" : [ { "count" : 16 }, { "count" : 16 } ] }, { "count" : 15 } ] } } ], "timemillis" : 38, "counts" : { "input" : 130, "emit" : 130, "reduce" : 5, "output" : 6 }, "ok" : 1 } i don't know why got multidimensional array sec argument reduce function. mean part of result:
{ "_id" : { "country_code" : "1", "distribution" : 405, "installationperiod" : 4 }, "value" : { "count" : [ { "count" : [ // <= why multidimensional? { "count" : 16 } why multidimensional? , why key of embedded array same returned reduce function?
the reason because mapreduce works. documentation point:
mongodb can invoke cut down function more 1 time same key. in case, previous output cut down function key become 1 of input values next cut down function invocation key.
and later point:
the type of homecoming object must identical type of value emitted map function ensure next operations true:
so though have not "changed signature" documentation points to, still processing n items @ 1 time in 1 cut down pass , n items in next pass. happens in eventual processing of array returned in 1 fragment combined array fragment.
so happened cut down returns array, not "all" of items emitted key, of them. cut down on same "key" processes more items. 2 arrays (or more) 1 time again sent reduce, in effort "reduce" items intended.
that general concept, no surprise when pushing array get.
short version, mapreduce processes ouput "keys" in chunks , not @ once. improve larn before becomes problem later.
mongodb multidimensional-array mapreduce
No comments:
Post a Comment