pymongo mapreduce db断言失败

时间:2011-03-28 17:21:33

标签: mapreduce pymongo

我刚刚开始使用mongo和map / reduce,并且在使用pymongo时遇到以下错误,直接使用mongo命令行时我没有得到(我意识到有一个类似的问题这个,但我的看起来更基本了。)

我直接使用pymongo文档中的示例:http://api.mongodb.org/python/1.3%2B/examples/map_reduce.html

from pymongo import Connection
from pymongo.code import Code

db = Connection().map_reduce_example
db.things.insert({"x": 1, "tags": ["dog", "cat"]})
db.things.insert({"x": 2, "tags": ["cat"]})
db.things.insert({"x": 3, "tags": ["mouse", "cat", "dog"]})
db.things.insert({"x": 4, "tags": []})


m = Code("function () {this.tags.forEach(function(z) {emit('d, 1);});}")
m = Code("function () {emit('dog', 1);}")

r = Code("function (key, values) {var total = 0;for (var i = 0; i < values.length; i++) {total += values[i];}return total;}")


result = db.things.map_reduce(m, r)

这给了我以下错误:

Traceback (most recent call last):
  File "demo.py", line 17, in <module>
    result = db.things.map_reduce(m, r)
  File "/usr/local/lib/python2.6/dist-packages/pymongo-1.9-py2.6-linux-i686.egg/pymongo/collection.py", line 943, in map_reduce
    map=map, reduce=reduce, **kwargs)
  File "/usr/local/lib/python2.6/dist-packages/pymongo-1.9-py2.6-linux-i686.egg/pymongo/database.py", line 293, in command
    msg, allowable_errors)
  File "/usr/local/lib/python2.6/dist-packages/pymongo-1.9-py2.6-linux-i686.egg/pymongo/helpers.py", line 119, in _check_command_response
    raise OperationFailure(msg % response["errmsg"])
pymongo.errors.OperationFailure: command SON([('mapreduce', u'things'), ('map', Code("function () {emit('dog', 1);}", {})), ('reduce', Code('function (key, values) {var total = 0;for (var i = 0; i < values.length; i++) {total += values[i];}return total;}', {}))]) failed: db assertion failure

但是,在mongo中定义map / reduce函数并运行以下命令可以:

db.things.mapReduce(mm,r,{out:{inline:1}})

定义差异似乎是{out:{inline:1}}选项。有没有办法让它进入pymongo的电话?

谢谢,

亚当。

1 个答案:

答案 0 :(得分:5)

问题与'out'参数有关。从MongoDB 1.7.4开始,需要'out'参数,在调用map_reduce时必须始终指定输出集合的名称。

result = db.things.map_reduce(m, r, "output_collection_name")

如果要在内存中执行整个MapReduce操作,请调用inline_map_reduce。

result = db.things.inline_map_reduce(m, r)

以上示例仅适用于直接从git存储库获取的最新版本的pymongo。 pymongo 1.9与MongoDB 1.7.4或更高版本不兼容。