问题描述
所以我是 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:
{'key':5, 'value':5}
{'key':5, 'value':4}
{'key':5, 'value':4}
{'key':5, 'value':1}
{'key':5, 'value':1}
{'key':4, 'value':6}
{'key':4, 'value':6}
{'key':4, 'value':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, '值': 11.0}
{'_id':4, 'value': 11.0}
{'_id':5, '值': 11.0}
{'_id':5, 'value': 11.0}
如您所见,对于键 4 &5 我得到了 11 的预期答案,但是对于键 3(集合中只有一个具有该键的条目)我得到了意想不到的 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函数将具有相同key的文档合并为一个文档.如果 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!