我搜索了各种论坛,但没有找到确切答案。关于bson.code,Pymongo的文档非常少,没有示例。
实际上,我想将诸如list之类的Python变量传递给map_reduce函数的scope参数。
这是我的Python代码:
import pymongo
from pymongo import MongoClient
from bson.code import Code
client = MongoClient('mongodb://localhost/mydb')
db = client['mydb']
stopwords = ["The", "is", "at"]
mapper = Code("""
function map() {
// Some code here for mapper function....
}
""")
reducer = Code("""
function reduce(key, values) {
// Some code here for reducer function...
}
""")
output_collection = db.MediaSummary.map_reduce(
mapper, reducer, 'SearchWords',
scope = {
"stop-words": stopwords # This is line 91
}
)
执行时出现以下错误:
Traceback (most recent call last):
File "e:/Scripts/Python/VideoData_SearchWords_MapReduce.py", line 91, in <module>
"stop-words": stop_words
File "C:\Users\Faisal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 2773, in map_reduce
user_fields=user_fields)
File "C:\Users\Faisal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\collection.py", line 252, in _command
user_fields=user_fields)
File "C:\Users\Faisal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\pool.py", line 584, in command
user_fields=user_fields)
File "C:\Users\Faisal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\network.py", line 158, in command
parse_write_concern_error=parse_write_concern_error)
File "C:\Users\Faisal\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pymongo\helpers.py", line 155, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: exception: SyntaxError: Unexpected identifier
我不知道如何在这里使用bson.code。我猜可能是这个问题。但是无论如何都需要知道如何解决此代码?