2012-03-21 19 views
1

我需要在MongoDB的python代碼中執行動態創建的map/reduce javascript函數(在python中,我創建了字符串 - 用於map/reduce的javascript代碼)。如何在Python代碼中調用這些字符串(JavaScript函數)並執行?在Python/PyMongo中執行JavaScript代碼map/reduce

回答

1

您將需要使用pymongo.code.Code你實例使用JavaScript代碼爲按以下方式單一字符串參數Code對象:

maper = Code('function() { for (var key in this) { emit(key, 1); }}') 
reducer = Code('function(key, values) { return 1; }') 
result = collection.map_reduce(maper, reducer, 'results') 

resultCollection實例將包含地圖的結果/reduce.I已經在mapperreducer中加入了一些虛擬代碼來說明這一點。有關更多信息,請參閱pymongo文檔中的Map/Reduce Example