MongoDB MapReduce-发射一键/一值不调用reduce [英] MongoDB MapReduce - Emit one key/one value doesnt call reduce
问题描述
因此,我一般对mongodb和mapreduce并不陌生,并且遇到了这个怪癖"问题.(或者至少在我看来是个怪癖)
So i'm new with mongodb and mapreduce in general and came across this "quirk" (or atleast in my mind a quirk)
说我的收藏夹中有这样的对象:
Say I have objects in my collection like so:
{'键':5,'值':5}
{'key':5, 'value':5}
{'键':5,'值':4}
{'key':5, 'value':4}
{'键':5,'值':1}
{'key':5, 'value':1}
{'键':4,'值':6}
{'key':4, 'value':6}
{'键':4,'值':4}
{'key':4, 'value':4}
{'key':3,'value':0}
{'key':3, 'value':0}
我的 map 函数只是发出键和值
My map function simply emits the key and the value
我的 reduce 函数仅将值添加 AND ,然后将它们加1(我这样做是为了检查reduce函数是否被调用)
My reduce function simply adds the values AND before returning them adds 1 (I did this to check to see if the reduce function is even called)
我的结果如下:
{'_ id':3,'value':0 }
{'_ id':4,'value':11.0}
{'_id':4, 'value': 11.0}
{'_ id':5,'value':11.0}
{'_id':5, 'value': 11.0}
如您所见,对于键4&5我对键3得到了预期的11 BUT答案(在该键集合中只有一个条目),我得到了意外的0!
As you can see, for the keys 4 & 5 I get the expected answer of 11 BUT for the key 3 (with only one entry in the collection with that key) I get the unexpected 0!
这是mapreduce的自然行为吗?对于MongoDB?对于pymongo(我正在使用)?
Is this natural behavior of mapreduce in general? For MongoDB? For pymongo (which I am using)?
推荐答案
reduce函数将具有相同键的文档组合到一个文档中.如果map函数针对特定键发出单个文档(与键3一样),则不会调用reduce函数.
The reduce function combines documents with the same key into one document. If the map function emits a single document for a particular key (as is the case with key 3), the reduce function will not be called.
这篇关于MongoDB MapReduce-发射一键/一值不调用reduce的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!