MongoDB Map/Reduce 引发异常:失败:数据库断言失败

发布于 2024-10-08 02:19:17 字数 2435 浏览 0 评论 0原文

我创建了映射/归约函数来将任务结果分组到一个结果对象中。我使用 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})

它工作得很好。

问题可能出在哪里?

I created map/reduce functions to group tasks results in one result object. I wrote in in python with using pymongo library:

    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
    }""")

And i need that result object be in the same order as input tasks queries. But when i use sort param in request:

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

But this raise exception in 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

When i use the same query without sort param:

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

It's work pretty fine.

Where can be the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2024-10-15 02:19:17

我发现了我的问题。正如一位开发人员所说,没有索引我们就无法使用排序。因此,如果您使用排序,首先必须为该排序创建索引。

I found my problem. As was said by one of developers, we can't use sorting without indexes. So if you use sorting, first of all you must create an index for that sorting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文