2011-09-15 48 views
3

我無法弄清楚我在這裏做錯了什麼。我使用pymongo並具有以下的map/reduce代碼(該文件的所有屬性都可以直接訪問 - 即沒有預埋件與此有關:pymongo用戶聲明:13606:'out'必須是字符串或對象

(文件getTableMap.js):

function() { 
    var tablePoints1 = 0; 
    var tablePoints2 = 0; 
    if (this.pointsTeam1 == this.pointsTeam2) { 
    tablePoints1 = 1; 
    tablePoints2 = 1; 
    } 
    else { 
    if (this.pointsTeam1 > this.pointsTeam2) { 
     tablePoints1 = 3; 
    } 
    else { 
     tablePoints2 = 3; 
    } 
    } 
    emit(this.idTeam1, [tablePoints1, this.pointsTeam1, this.pointsTeam2]); 
    emit(this.idTeam2, [tablePoints2, this.pointsTeam2, this.pointsTeam1]); 
} 

的它調用map_reduce Python代碼看起來是這樣的:

def getTableOnMatchday(self): 
    m = Code(open('getTableMap.js','r').read()) 
    r = Code("""function(k,values) { 
    var foo = 'foo'; 
    return(foo); 
    }""") 

    result = bl_1.map_reduce(m, r, "myresult") 
    for doc in result.find(): 
    print doc 

對於Python代碼我適應直接從文檔的簡單例子: http://api.mongodb.org/python/current/examples/map_reduce.htmlMap Reduce example from pymongo 2.0.1 documentation

Python的回溯,當我運行代碼,我得到的是:

>>> api.getTableOnMatchday() 
    Traceback (most recent call last): 
    pymongo.errors.OperationFailure: command SON([('mapreduce', u'bl1_2011'), 
    ... 
    ... 
    ... 
) failed: db assertion failure 

這並沒有確切地告訴我很多,所以我把上詳細的日誌記錄的mongod,發現這個在日誌中:

Thu Sep 15 21:04:02 [conn7] User Assertion: 13606:'out' has to be a string 
or an object 

從查看實際生成map_reduce調用的Python代碼,第三個參數('out',根據pymongo 2.0.1文檔)是'myresult',這當然是一個字符串。

pymongo在這裏抱怨什麼? Javascript在語法上是正確的(我認爲)。我知道reduce現在什麼都不做,但是這不應該阻止編譯命令serverside - 或者它可以嗎?

回答

6

我想我已經找到了答案,通過更多的試驗和錯誤,並通過閱讀文檔的PHP驅動程序:

result = bl_1.map_reduce(m, r, out="foo") 

實際上,你必須指定OUT =字符串作爲第三個參數。

文檔中的例子在這裏誤入歧途導致,因爲它說的事:

result = bl_1.map_reduce(m, r, "foo") 
0

MapReduce的輸出選項: 預V1.8:如果沒有指定出的值,然後將結果被放入臨時集合中,其名稱將在命令輸出中給出(見下文)。否則,您可以爲out選項指定集合的​​名稱,並將結果放在那裏。

v1.8 +:輸出選項已更改。 Map-reduce不再生成臨時集合(因此,keepTemp已被刪除)。

更多的信息可以發現here

相關問題