python - MongoDB Map/Reduce 引发异常 : failed: db assertion failure

标签 python mongodb mapreduce pymongo

我创建了 map/reduce 函数来将任务结果分组到一个结果对象中。我使用 pymongo 库在 python 中编写:

    m = Code("""function() {
        data = {};
        res = ''
        if(this.result_id) {
            res={'objectid':this.result_id.toString()};
        } else {
            res=this.result;
        }
        emit(this.data, res);
    }""")
    r = Code("""function(k,values) { 
        data={};
        for(var i=0; i<values.length; i++ ) {
            for(attr in values[i])
                data[attr]=values[i][attr];
        }
        return data
    }""")

而且我需要结果对象与输入任务查询的顺序相同。但是当我在请求中使用排序参数时:

   res = db.tasks.map_reduce(m, r, query={'job_id':job_id},sort={'position':pymongo.ASCENDING})

但这会在 mongodb 中引发异常:

    Traceback (most recent call last):
      File "/usr/local/lib/python2.6/dist-packages/gevent/greenlet.py", line 403, in run
        result = self._run(*self.args, **self.kwargs)
      File "/data/www/public/app/seotools/daemon/scripts/mainconverter.py", line 129, in work
        res = autoreconnect(self.db.tasks.map_reduce,m, r, query={'job_id':job_id},sort={'position':1})
      File "/data/www/public/app/seotools/daemon/lib/db/mongo.py", line 95, in autoreconnect
        result = func(*args,**kwargs)
      File "/usr/local/lib/python2.6/dist-packages/pymongo-1.8.1-py2.6-linux-x86_64.egg/pymongo/collection.py", line 945, in map_reduce
        map=map, reduce=reduce, **kwargs)
      File "/usr/local/lib/python2.6/dist-packages/pymongo-1.8.1-py2.6-linux-x86_64.egg/pymongo/database.py", line 294, in command
        (command, result["errmsg"]))
    OperationFailure: command SON([('mapreduce', u'tasks'), ('sort', {'position': 1}), ('query', {'job_id': ObjectId('4d0b30909c7684b60e000000')}), ('reduce', Code('function(k,values) { 
        data={};
        for(var i=0; i<values.length; i++ ) {
            for(attr in values[i])
                data[attr]=values[i][attr];
       }
        return data
    }', {})), ('map', Code("function() {
        data = {};
        res = ''
        if(this.result_id) {
            res={'objectid':this.result_id.toString()};
        } else {
            res=this.result;
        }
        emit(this.data, res);
    }", {}))]) failed: db assertion failure

当我使用没有排序参数的相同查询时:

   res = db.tasks.map_reduce(m, r, query={'job_id':job_id})

它工作得很好。

问题出在哪里?

最佳答案

我发现了我的问题。正如一位开发人员所说,我们不能在没有索引的情况下使用排序。因此,如果您使用排序,首先您必须为该排序创建一个索引。

关于python - MongoDB Map/Reduce 引发异常 : failed: db assertion failure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4469693/

相关文章:

python - 如何告诉 Python 脚本运行后关闭

c# - 使用 C# 将多个文档插入 Mongodb 的最快方法

python - Hadoop Streaming "comparator.options"未被尊重

hadoop - mapreduce 程序 : Reducer is not getting called

python - 如何从 Data Science Experience 上的 Python 访问 Bluemix Object Storage 上的文件?

python - Pandas - 根据列中的列表从数据框中选择行

python - 在 Python 中读取 .lnk 文件的目标?

mongodb - 如何导入官方mongodb驱动包

mongodb - 为什么匹配聚合查询中的日期被忽略?

hadoop - mapreduce 中组合器和映射器内组合器的区别?