是否可以在pymongo中的Collection.map_reduce的scope参数中传递javascript函数?

时间:2012-04-01 13:37:44

标签: mongodb pymongo

假设:

jstrMap = """
function() {
  print("isPointInside = " + isPointInside);
  print("polygon = " + polygon);
  emit(this._id, this);
}
"""
jstrReduce = """
function(key, values) {
  return values[0];
}
"""

def readJSCodeFromFile(filePath):
  with open(filePath) as f:
    return Code(f.read())

jsIsPointInside = readJSCodeFromFile(path.join(path.dirname(__file__), 'IsPointInside.js'))

IsPointInside.js:

function(pt, poly) {
}

我像这样调用map_reduce:

mycoll.map_reduce(jstrMap, jstrReduce, 'results',
    scope = {'isPointInside': jsIsPointInside, 'polygon': [[-77, 39], [-77,38], [-78,38], [-78,39]]})

以下是我在客户端控制台上获得的内容:

db assertion failure, assertion: 'map invoke failed: JS Error: TypeError: isPointInside is not a function nofile_b:3', assertionCode: 9014

服务器输出为:

isPointInside = null
polygon = -77,39,-77,38,-78,38,-78,39
Sun Apr 01 16:29:14 [conn11] JS Error: TypeError: isPointInside is not a function nofile_b:3
Sun Apr 01 16:29:14 [conn11] mr failed, removing collection :: caused by :: 9014 map invoke failed: JS Error: TypeError: isPointInside is not a function nofile_b:3

调试python代码表明jsIsPointInside的类型为Code,正如预期的那样。 str(jsIsPointInside)返回函数文本,即'function(pt,poly){\ n} \ n'

我不想填充system.js集合,我想在范围内传递函数。它有可能吗?

感谢。

1 个答案:

答案 0 :(得分:0)

Scope是一个对象,其中字段作为变量与字段名称一起放在MapReduce范围内。

如果你想将一个函数放在范围内,你需要将它作为一个字段,例如。

scope = {
    myFunc: function() { return "Foo";} 
}