Mapreduce 在Python/PyMongo-map/reduce中执行JavaScript代码
我需要在MongoDB上的python代码中执行动态创建的map/reduce javascript函数(在python中,我为map/reduce创建字符串javascript代码)。如何在Python代码中调用这些字符串(javascript函数)并执行?您需要使用javascript代码作为单个字符串参数,以以下方式实例化Mapreduce 在Python/PyMongo-map/reduce中执行JavaScript代码,mapreduce,pymongo,Mapreduce,Pymongo,我需要在MongoDB上的python代码中执行动态创建的map/reduce javascript函数(在python中,我为map/reduce创建字符串javascript代码)。如何在Python代码中调用这些字符串(javascript函数)并执行?您需要使用javascript代码作为单个字符串参数,以以下方式实例化code对象: maper = Code('function () { for (var key in this) { emit(key, 1); }}') reduce
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')
其中,result
是一个Collection
实例,它将包含映射/reduce的结果。为了说明这一点,我在mapper
和reducer
中都添加了一些伪代码。您可以在pymongo
文档中查阅更多信息。您需要使用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')
其中,result
是一个Collection
实例,它将包含映射/reduce的结果。为了说明这一点,我在mapper
和reducer
中都添加了一些伪代码。有关更多信息,请参阅pymongo
文档