Mongodb 是否可以在pymongo中Collection.map_reduce的scope参数中传递javascript函数?
鉴于: IsPointInside.js:Mongodb 是否可以在pymongo中Collection.map_reduce的scope参数中传递javascript函数?,mongodb,pymongo,Mongodb,Pymongo,鉴于: IsPointInside.js: jstrMap = """ function() { print("isPointInside = " + isPointInside); print("polygon = " + polygon); emit(this._id, this); } """ jstrReduce = """ function(key, values) { return values[0]; } """ def readJSCodeFromFile(fi
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'))
我这样调用map_reduce:
function(pt, poly) {
}
以下是我在客户端控制台上得到的信息:
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
调试python代码会发现jsIsPointInside是代码类型,正如预期的那样。strjsIsPointInside返回函数文本,即'functionpt,poly{\n}\n'
我不想填充system.js集合,我想在作用域中传递函数。有可能吗
谢谢。范围是一个对象,其中字段作为变量和字段名放置在MapReduce范围中 如果你想把一个函数放到作用域中,你需要把它变成一个值,一个字段,例如
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。我不能使用javascript语法。使用python语法,我正在做您建议的事情—请注意“isPointInside”范围参数。map_reduce命令需要kwargs,其中一个可能是scope。
scope = {
myFunc: function() { return "Foo";}
}