开发者

pymongo mapreduce db assertion failure

开发者 https://www.devze.com 2023-02-20 13:18 出处:网络
I\'m just getting started with mongo and map/reduce, and I\'m coming into the following error when using pymongo that I don\'t get when using the mongo command line directly (I realize there is a simi

I'm just getting started with mongo and map/reduce, and I'm coming into the following error when using pymongo that I don't get when using the mongo command line directly (I realize there is a similar question to this one, but mine seems far more basic).

I'm using the example directly from the pymongo documentation: 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": 开发者_JS百科[]})


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)

This gives me the following error:

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

However, defining the map/reduce functions in mongo and running the following command works:

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

The defining difference seems to be the {out:{inline:1}} option. Is there a way to get that into the call in pymongo?

Thanks,

Adam.


The problem is related to the 'out' parameter. Starting with MongoDB 1.7.4 the 'out' parameter is required, you must always specify the name of the output collection when invoking map_reduce.

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

If you want to perform your whole MapReduce operation in memory, call inline_map_reduce instead.

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

The above examples only work with the latest version of pymongo directly got from the git repository. pymongo 1.9 doesn't work well with MongoDB 1.7.4 or superior.

0

精彩评论

暂无评论...
验证码 换一张
取 消