如何在 java 中调用 mapReduce 中的 mongodb 服务器端函数

how to call mongodb server side function inside mapReduce in java

我在 "system.js" 集合中存储了两个函数,名为 "mapfun" 和 "reducefun",我正在尝试从 java 调用这些函数。我正在尝试通过 MapReduceCommand 调用这些函数。我无法调用这些函数。谁能帮我解决这个问题。 这两个函数看起来像这样 //mapfun

{ "_id" : "mapfun","value" : { "code" : "function(){var criteria; if(this.speed > 70 ){ criteria="overspeed";emit(criteria,this.speed); } }" } }

//减少乐趣

{ "_id" : "reducefun", "value" : { "code" : "function(key,speed){ var total=0; for(var i=0;i<speed.length;i++){ total=total+speed[i]; } return total/speed.length; }" } }

我的 mapreduce 命令看起来像这样

MapReduceCommand command=new MapReduceCommand(collection, map, reduce, null, MapReduceCommand.OutputType.INLINE, null);
MapReduceOutput output=collection.mapReduce(command);

我已经将 map 和 reduce 函数作为字符串传递,我在其中分别调用了 mapfun 和 reducefun 它看起来像这样

String map = "function (){var x=mapfun();"
                + "return x;};";
String reduce = "function(key, speed) {var y=reducefun(key,speed);"
                + "return y;};";

我在这里做错了什么以及如何纠正这个请帮助我解决这个问题。

潜在的问题是 this 变量为每个函数调用重新分配。

为了演示,我 运行 来自 mongo shell 的简短测试。

先创建一个包含 1 个文档的集合:

MongoDB Enterprise replset:PRIMARY> db.maptest.find({},{_id:0})
{ "canary" : "tweet" }

然后创建一个存储函数,它将接受一个参数并发出 2 个值,一个使用 this,一个使用传递的参数:

MongoDB Enterprise replset:PRIMARY> db.system.js.find()
{ "_id" : "testfun", "value" : { "code" : "function(arg){emit(\"this.canary\",this.canary),emit(\"arg.canary\",arg.canary)}" } }

然后在 mapReduce 调用中使用存储的函数:

MongoDB Enterprise replset:PRIMARY> db.maptest.mapReduce(function(){testfun(this)},function(a,b){return {a:a,b:b}},{out:{inline:1}})
{
    "results" : [
        {
            "_id" : "arg.canary",
            "value" : "tweet"
        },
        {
            "_id" : "this.canary",
            "value" : undefined
        }
    ],
...

如您所见,this.canary 未定义,但 arg.canary 包含输入文档中的值。

mapReduce 框架在调用 map 函数时将 this 分配给当前正在检查的文档。当从 map 函数中调用存储函数时,它会获得自己的 this。但是,通过将函数作为 testfun(this) 调用,map 函数的原始 this 上下文作为参数提供给存储的函数。