2
我創建了map/reduce函數以將任務分組結果放入一個結果對象中。我使用pyymongo庫在Python中寫入:MongoDB Map/Reduce引發異常:失敗:db斷言失敗
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
}""")
而且我需要該結果對象與輸入任務查詢的順序相同。但是當我使用排序PARAM中要求:
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
當我使用沒有排序PARAM相同的查詢:
res = db.tasks.map_reduce(m, r, query={'job_id':job_id})
它的工作相當精細。
哪裏可以解決問題?
當你首先進行排序會發生什麼? 'res = db.tasks.map_reduce(m,r,sort = {'position':pymongo.ASCENDING},query = {'job_id':job_id})'應該沒關係,但是如果有的話,您可能已經找到一個錯誤? – 2010-12-17 18:02:00
我會試試這個,讓你知道 – 2010-12-19 08:21:28
ammm ......你說得對,如果params的改變順序有效。 – 2010-12-20 09:12:50